Let me give you my heart and we'll learn this together! 🔥UNITY SAVINGS: bit.ly/UnitySalesHub 🔥SpeedTutor Unity Store: bit.ly/STUnityStorePuzzlePacks 🔥HUMBLE SAVINGS: bit.ly/HumbleBundleDeals
hi, for anyone that hasn't realized yet, if you change the " - bombDamage" or whatever other name you're using, for a " + bombDamage" you can create aswell a healing script if you want to put some extra lifes in your game, also thanks a lot for the tutorial :D
@@SpeedTutor ye thank u so much I figure it by my own after some time!! Btw I just want you u to know that u saved my life because in 6 days I need to send my videogame to my teacher and I was stuck on the hearts for like 3 days
Helpp! I have done everything in this video as he said, but If i touch the bomb it just gets pushed by the player. I have checked the skript multiple times but everyting is correct. The health system works aswell, as i have testet it the same as he did. Please help!
Hi! A couple of days ago I finished with your unity survival game series and I'm very happy with the results, thank you for your time and effort! Can you please make a video on a proper character death? Like when your thirst or hunger bar is at 0 you die of hunger/ thirst, would appreciate it
I really appreciate it! What do you want to learn? Just check whenever you update your health, when it reaches 0 you do whatever you want. Create a fail screen, play an animation, load a new scene? What do you want to happen?
@@SpeedTutor Thank you for The reply! I was thinking of like a basic death screen, you know when the character dies, he drops down to the ground and then a text is displayed on the screen saying 'You died' and Then after a couple of seconds he respawns back to the starter point
@@SpeedTutor I did check out that video, it was a cool video for a base of the death screen. Anyways thanks. I hope once that video Is out it would help others too. Thanks for replying!
You using a new editor? Did you get this idea from my black and white problem? I've thought about doing it this way, but I wouldn't work the way I want it.
@@SpeedTutor no remember I was asking how to make the screen black and white, and you suggested using lut, in post processing? No I haven't sorted it yet.
Oh haha! I did create a new intro and I've been trying to improve the overall quality of my videos. The channel is really struggling so I'm trying to make everything as good as it possibly can! :)
If you run into a problem where a player goes through an object that was meant to damage them but it doesn't, try this. In the damage controller, change the code to this. void OnTriggerEnter(Collider other) { _healthController.playerHealth = _healthController.playerHealth - badCoinDamage; _healthController.UpdateHealth(); gameObject.SetActive(false); Destroy(gameObject); // Optional, destroy game object on collision } } change the variable names to your own.
Dang. This is how I learned that you can't add Scene Objects to prefabs. Rather than using "bomb damage" for my game, I created enemies that would do damage and take away a heart from the player on collision. After following this tutorial, I found out that as long as I have an Enemy Object already placed in the game, it works. But if I have this Enemy as a Prefab, I can't add the "Health Controller" to the Damage Controller script that is attached to my prefab. Surely there is a way around this (it looks like you can do this through a script, but was stated that this is slow). Any other suggestions to have this work? I guess the point is that prefabs can only reference other prefabs and not Scene Objects because if you change Scenes, it wouldn't have anything to reference... so why not just create the Scene Object in each scene if you are using the prefab in each scene and allow to apply Scene Objects to prefabs???
Hey there, I had the same problem like you where when I went to attach the 'Health Controller' game object to my Damage Controller script, it wouldn't let me and would instead say 'Type Mismatch' when I tried selecting it... You said you solved by creating enemies and taking away a heart from the player BUT HOW...? did you make an entirely new enemy??
WOW thanks for the super quick reply and GOOD NEWS - IT WORKS, my game finally works, i just did what you said and BOOM it worked soo to you i owe a big thank you :D
@@mrlaz9011 ahh that happens somethings, dw thou i shall paste it here for you: Oh it's been a minute now and I have a lot of things that happen when my player takes damage, but I had to basically just reverse the way I thought about it. Rather than thinking "When this enemy touches the Player, cause damage to Player" I instead did "When Player touches the enemy, cause damage to the Player". It's the exact same thing, but before, I was trying to attach this script and a Game Object to a prefab and that just does not work. It doesn't allow that. But you can attach a Game Object to another Object (like the Player in my case). So don't try to attach a script and Game Object to a prefab, attach it to something else in your game. I also did some wonky stuff to make it work with other things, like making one script reference another script. I'm still super new at coding, so may want to ask someone else for help, especially cause as I go back over my own code, it's pretty messy. I have scripts like "PlayerHealth", ""DamageController", "Enemy", "EnemySpawner", "Health", that are all referencing each other and have a lot of different if statements to trigger certain things, that all probably could have been done in much more simple and elegant way.
Hello, I just have one problem when I write playerhealth or Updatehealth in the DamageControll script it does not recognize it. Do you have an idea of why this is a problem? Good tutorial btw.
Hello, It Works For Me But I Wanna Ask, Can I Disappear Hearts Instead Of Turning Black. Can You Help Me ? edit: I Got It Working Thanks For The Tutorial Again !
Hi i know this video is a year ago but what do i do when i want to set the health controller to a prefab thats in assets and not in the scene since its not letting me set it
OHH I had this same problem and fixed it a little while ago by simply reversing the idea instead of having it so when 'enemy touches player, player takes damage' I did 'When Player touches enemy, player takes damage' as you can see its the EXACT same thing. In Unity create a new tag called 'Enemy' and the in your code where it says compare.tag("Player") change it to compare.tag("Enemy") and then attach this script to the players gameobject (your main character). hope this helped and isn't too late :)
Is it just me but does it just refuse to work for some people like it say i have the code completely correct but for the love of god it just won't work
@@SpeedTutor the code itself had no error, it seem to be more of the collision not registering, i tried it twice, first followed you exactly it didn’t work, I scraped it then tried again but this time I used the health system from a black thorn prod which didn’t change much aside from having an empty heart sprite, still didn’t work and now I’m just stumped
It doesn't let me drag the Healthcontroller GameObject into the script once I add it to my Prefab (which is just a 3d sphere with a rigidbody and sphere collider) Any idea why?
It's worth noting if I drop the prefab sphere into the scene hierarchy I can apply the health controller just fine. But as soon as I try to save it as a prefab it removes itself
It's not working for me. I figured out that the Box Collider2D has to be set to Is Trigger, but then my enemy will just fall through the floor, which is a problem because It's not a static enemy. The other issue, when I lock my enemy on all axis(as is your bomb) and set it to Is Trigger, is that the health does not update when I trigger with the collider of the enemy the enemy is destroyed though.
@@SpeedTutor what do you mean a parent collider. I have the same problem. Everything works fine. It's just I don't want a static enemy. I want to add this script to enemies that have an AI path and movement.
@@jeremystonecipher4159 If you have two box colliders to your AI, scale one of them down a bitt and untick "Is Trigger" On the bigger Box collider check "Is Trigger. This should work with a moving AI with rigid bodies. Let me know if it worked :)
@@SpeedTutor figured it out now it wouldnt work at first so i edited the script a little bit just to realize I didnt have "is trigger" ticked on my enemies haha
how do i fix IndexOutOfRangeException: Index was outside the bounds of the array. HealthController.UpdateHealth () (at Assets/HealthController.cs:35) HealthController.Start () (at Assets/HealthController.cs:15) my code is this pls tell me what the problem is using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class HealthController : MonoBehaviour { public int playerHealth; [SerializeField] private Image[] hearts; private void Start() { UpdateHealth(); } public void UpdateHealth() { if (playerHealth
Let me give you my heart and we'll learn this together!
🔥UNITY SAVINGS: bit.ly/UnitySalesHub
🔥SpeedTutor Unity Store: bit.ly/STUnityStorePuzzlePacks
🔥HUMBLE SAVINGS: bit.ly/HumbleBundleDeals
hi, for anyone that hasn't realized yet, if you change the " - bombDamage" or whatever other name you're using, for a " + bombDamage" you can create aswell a healing script if you want to put some extra lifes in your game, also thanks a lot for the tutorial :D
Thanks for the suggestion! Thanks for checking out the tutorial.
BRO IM STRUGGLING FOR 2 DAYS AND YOU SAVED ME THANK YOU
I'm very glad you found this and I could help you! :) What have you been working on?
@@SpeedTutor I'm working on an adventure rpg game and this video helped me a lot
Amazing! How is your progress?
@@SpeedTutor It's going pretty well. Thank you for asking 🙂
Awesome, best of luck with it! :D
For those who have problems with taking damage not working, make sure to set the players tag to "Player"
This right here
Very Helpful. Ran into some problems but in the end it worked out. Thanks!
Awesome, how did you fix it?
I was looking for this system all day long and ı cant resolve my game problem thank you so much again.Adamsın.
I hope you found this useful! :)
THANKS YOU SOO MUCH but how do i keep the bomb, and not make it disappear?
No worries, thanks for watching! :) Just make sure you don't Destroy the object, or .SetActive(false) - Depending on how I did it in the tutorial.
@@SpeedTutor ye thank u so much I figure it by my own after some time!! Btw I just want you u to know that u saved my life because in 6 days I need to send my videogame to my teacher and I was stuck on the hearts for like 3 days
Awesome, good job!
Thank you so much i really appreciate it i'm building a test game and this is just what i need
Glad I could help! What progress have you made?
this tutorial helped me so much! thanks!
I'm really glad it did, thanks for coming to watch! :)
Helpp! I have done everything in this video as he said, but If i touch the bomb it just gets pushed by the player. I have checked the skript multiple times but everyting is correct. The health system works aswell, as i have testet it the same as he did. Please help!
I have the same problem
Hi!
A couple of days ago I finished with your unity survival game series and I'm very happy with the results, thank you for your time and effort!
Can you please make a video on a proper character death? Like when your thirst or hunger bar is at 0 you die of hunger/ thirst, would appreciate it
I really appreciate it! What do you want to learn? Just check whenever you update your health, when it reaches 0 you do whatever you want. Create a fail screen, play an animation, load a new scene? What do you want to happen?
@@SpeedTutor Thank you for The reply!
I was thinking of like a basic death screen, you know when the character dies, he drops down to the ground and then a text is displayed on the screen saying 'You died' and Then after a couple of seconds he respawns back to the starter point
I do have a respawning tutorial which might get you started on that! :) I'll note this onto my list, thanks for the suggestion.
@@SpeedTutor I did check out that video, it was a cool video for a base of the death screen.
Anyways thanks. I hope once that video Is out it would help others too.
Thanks for replying!
thanks
You're very welcome, my friend!
thank you, it was very helpful!
I'm glad you think so, thanks for coming along to check this out! :D
Hi do you have any tips on how to make the hearts change sprites to a half heart instead of just turning black?
You could add more sprites to the array, but with them split in half? :)
omg¬¬¬¬thx ssssoooo much!!!!!!!!!!!!!!!!!!!!! u made this really clear and understandable
You're very welcome, Ava! Good luck with your project. :)
You using a new editor? Did you get this idea from my black and white problem? I've thought about doing it this way, but I wouldn't work the way I want it.
New Unity Editor? This is 2019.4 or 2020, I can't remember! Haha. Did you sort your colour issue?
@@SpeedTutor no remember I was asking how to make the screen black and white, and you suggested using lut, in post processing? No I haven't sorted it yet.
Yes, I remember! I'm not sure what you meant by new editor though?
@@SpeedTutor Oh yes I meant your intro looks more ZAPPY like your using a new VIDEO EDITOR ..sorry I should of explained better lol
Oh haha! I did create a new intro and I've been trying to improve the overall quality of my videos. The channel is really struggling so I'm trying to make everything as good as it possibly can! :)
Can you recreate your survival horror series it is outdated now : ( if not full then only some specific videos?
What would you like to see?
If you run into a problem where a player goes through an object that was meant to damage them but it doesn't, try this.
In the damage controller, change the code to this.
void OnTriggerEnter(Collider other)
{
_healthController.playerHealth = _healthController.playerHealth - badCoinDamage;
_healthController.UpdateHealth();
gameObject.SetActive(false);
Destroy(gameObject); // Optional, destroy game object on collision
}
}
change the variable names to your own.
Dang.
This is how I learned that you can't add Scene Objects to prefabs.
Rather than using "bomb damage" for my game, I created enemies that would do damage and take away a heart from the player on collision.
After following this tutorial, I found out that as long as I have an Enemy Object already placed in the game, it works.
But if I have this Enemy as a Prefab, I can't add the "Health Controller" to the Damage Controller script that is attached to my prefab.
Surely there is a way around this (it looks like you can do this through a script, but was stated that this is slow).
Any other suggestions to have this work?
I guess the point is that prefabs can only reference other prefabs and not Scene Objects because if you change Scenes, it wouldn't have anything to reference... so why not just create the Scene Object in each scene if you are using the prefab in each scene and allow to apply Scene Objects to prefabs???
Hey there, I had the same problem like you where when I went to attach the 'Health Controller' game object to my Damage Controller script, it wouldn't let me and would instead say 'Type Mismatch' when I tried selecting it... You said you solved by creating enemies and taking away a heart from the player BUT HOW...? did you make an entirely new enemy??
WOW thanks for the super quick reply and GOOD NEWS - IT WORKS, my game finally works, i just did what you said and BOOM it worked soo to you i owe a big thank you :D
how did he solve it? the comment disappeared.
@@mrlaz9011 ahh that happens somethings, dw thou i shall paste it here for you:
Oh it's been a minute now and I have a lot of things that happen when my player takes damage, but I had to basically just reverse the way I thought about it. Rather than thinking "When this enemy touches the Player, cause damage to Player" I instead did "When Player touches the enemy, cause damage to the Player". It's the exact same thing, but before, I was trying to attach this script and a Game Object to a prefab and that just does not work. It doesn't allow that. But you can attach a Game Object to another Object (like the Player in my case). So don't try to attach a script and Game Object to a prefab, attach it to something else in your game. I also did some wonky stuff to make it work with other things, like making one script reference another script. I'm still super new at coding, so may want to ask someone else for help, especially cause as I go back over my own code, it's pretty messy. I have scripts like "PlayerHealth", ""DamageController", "Enemy", "EnemySpawner", "Health", that are all referencing each other and have a lot of different if statements to trigger certain things, that all probably could have been done in much more simple and elegant way.
Great tutorial
Thanks very much! :)
Great tutorial :D
Thanks Stig! :D
@@SpeedTutor :D
its not working and i dont have any errors aswell and i added a Debug.Log("Hit"); in the OnTriggerEnter2D and i dont even get the Debug
Did you follow my Trigger event checklist tutorial? Does your character have a Rigidbody2D component?
Make sure to set the tag of your player character to " Player "
Hello, I just have one problem when I write playerhealth or Updatehealth in the DamageControll script it does not recognize it. Do you have an idea of why this is a problem? Good tutorial btw.
That's very strange, is the method public?
@@SpeedTutor I solved it, I was just not telling my script where it should be getting the playerhealth, Im sorry I was tired, but thank you
Hello, It Works For Me But I Wanna Ask, Can I Disappear Hearts Instead Of Turning Black. Can You Help Me ?
edit: I Got It Working Thanks For The Tutorial Again !
Awesome! What did you do?
@@SpeedTutor I disabled the Image components of hearts via code instead of turning black. The code is Something like this: hearts[i].enabled = false;
@@kutsall28 That's what I would have suggested! Awesome work around. :) You don't even need me!
@@SpeedTutor Thank You Man !
I'm looking to do a system like this but with half hearts, how would I go about doing that?
Split the hearts into two sprites each, then add more to the array! :)
amazing stuff
Thanks very much for watching! :)
Question! How can you add this to like a shop system where if you pay a certain amount you can increase your heart by 1?
You could probably use a list instead of an array, then you can add one to the list for what you buy in the shop and do it all in the same way! :)
how do i save the hearts though scenes and also how do i reset hearts when they hit 0?
You could use playerprefs for saving something simple. :)
Hi i know this video is a year ago but what do i do when i want to set the health controller to a prefab thats in assets and not in the scene since its not letting me set it
OHH I had this same problem and fixed it a little while ago by simply reversing the idea instead of having it so when 'enemy touches player, player takes damage' I did 'When Player touches enemy, player takes damage' as you can see its the EXACT same thing.
In Unity create a new tag called 'Enemy' and the in your code where it says compare.tag("Player") change it to compare.tag("Enemy") and then attach this script to the players gameobject (your main character). hope this helped and isn't too late :)
I just did that and it worked! thanks for posting this!
@@mrlaz9011 BRILLIANT!! It's great to see it helped someone in the end
did everything you did i got the scripts working but it does nothing when a object collides wiith my player
Did you tag the player?
@@SpeedTutor I did, I’ve tried adding the code to the player movement aswell but still the same result
Same for me it does not work and I checked EVERYTHING
Is it just me but does it just refuse to work for some people like it say i have the code completely correct but for the love of god it just won't work
Any errors?
@@SpeedTutor the code itself had no error, it seem to be more of the collision not registering, i tried it twice, first followed you exactly it didn’t work, I scraped it then tried again but this time I used the health system from a black thorn prod which didn’t change much aside from having an empty heart sprite, still didn’t work and now I’m just stumped
@@SpeedTutor could you tell me how you had your 2d collider and rigid body 2d was set up please
Just one collider2d and ridigbody2d on my player and the same on my other items! :)
Try setting the player tag to "Player"
yes please! :D
It doesn't let me drag the Healthcontroller GameObject into the script once I add it to my Prefab (which is just a 3d sphere with a rigidbody and sphere collider) Any idea why?
Any errors messages in the console?
@@SpeedTutor Nope. When I click and drag over the controller it just doesn’t let me drop it into the slot
@@SpeedTutor any thoughts? If not I’ll post it to the unity forums to see if they can help me. 👍🏻
It's worth noting if I drop the prefab sphere into the scene hierarchy I can apply the health controller just fine. But as soon as I try to save it as a prefab it removes itself
You Have To Make The Script A Prefab
God bless you 🙌❤️
Thanks man! :)
how to regain a heart if we collide with a heart sprite in game
Just do the opposite to bombDamage, do += bombDamage or create a new variable called "healthToHealth" :)
I can’t get the damage to work I have everything copied exactly and the object doesn’t deal the damage or disappear
Did you attach the correct components?
Set the players tag to " Player "
@@SpeedTutor I got it to work I made 2 box colliders and put is trigger on one
@@dxrku7290 Thanks so much!! I just had the same problem and you're such a life saver
@@dxrku7290 two box colliders on enemy or one on player and one on enemy?
its not working. the heart is not turning black
Any errors?
@@SpeedTutor no its ok now its working properly thanks awesome tutorial😍😍😍
It's not working for me. I figured out that the Box Collider2D has to be set to Is Trigger, but then my enemy will just fall through the floor, which is a problem because It's not a static enemy. The other issue, when I lock my enemy on all axis(as is your bomb) and set it to Is Trigger, is that the health does not update when I trigger with the collider of the enemy the enemy is destroyed though.
You could add a parent collider to the object as well?
@@SpeedTutor what do you mean a parent collider. I have the same problem. Everything works fine. It's just I don't want a static enemy. I want to add this script to enemies that have an AI path and movement.
@@jeremystonecipher4159 If you have two box colliders to your AI, scale one of them down a bitt and untick "Is Trigger" On the bigger Box collider check "Is Trigger. This should work with a moving AI with rigid bodies. Let me know if it worked :)
@@michael.corlenome It did! thank you.
Does this still work? if so. WHY AM I GETTING SO MANY ERRORS!
It does work, what errors are you having?
@@SpeedTutor enough to have had forced a factory reset on my pc
@@jag-ca dang
it keeps saying for me that the name "_healthController" doesnt exist i have no idea why
Is it because your variable has a different name?
@@SpeedTutor figured it out now it wouldnt work at first so i edited the script a little bit just to realize I didnt have "is trigger" ticked on my enemies haha
Haha, good work! Usually it's a tiny thing that can cause the most annoying issues.
@@SpeedTutor I know right thanks for the tutorial anyway man
Its not working. the Player is not taking damage.
That's good, isn't it?
@@SpeedTutor I wrote wrong
Did you mean something else?
it dosent work for me aswell the player dosent take damage when he hit the bomb
its not working my heart is not going black
When you take damage? Any errors?
@@SpeedTutor I have not gotten there yet. its not working after the first script (google translate. im swedish)
Let me know, good luck! :)
its not working
What happens?
Maybe you didn't set the players tag to " Player "
how do i fix
IndexOutOfRangeException: Index was outside the bounds of the array.
HealthController.UpdateHealth () (at Assets/HealthController.cs:35)
HealthController.Start () (at Assets/HealthController.cs:15)
my code is this pls tell me what the problem is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class HealthController : MonoBehaviour
{
public int playerHealth;
[SerializeField] private Image[] hearts;
private void Start()
{
UpdateHealth();
}
public void UpdateHealth()
{
if (playerHealth