Nvm found one. I got a script where if i die i get respawned via prefabs. But if i do all the hearts in the [ ] array are gone. Do you know how i could fix that?
@@Fwsr. Glad to hear that you found some graphics assets! As for the problem you describe, this is definitely happening because your prefab doesn't know what heart images it is supposed to interact with. I can think of two main ways to fix this: 1) Don't use a prefab for your character. Instead of spawning a new character, just teleport the existing one back to a spawn point and set its health back to full. (This would be the easiest fix). 2) Since you can't drag your heart images into your prefab's script, you will need to code it. You can do this in your Start() function (so that it happens as soon as your character Instantiates into the scene). It would look something like: Hearts[1] = GameObject.Find(Heart1).GetComponent(); Hearts[1] refers to the array, so you'll need to do this for all of the hearts on your player. (Heart1) refers to the name of the game object (in your hierarchy) that you are trying to reference. I hope this helps! Good luck with your project.
It will work, but as with any scene change stuff, you have to decide how you want it to persist. For example, I would use DontDestroyOnLoad on both the UI and the player so that they persist when you scene change.
I got the whole script and everything to work, but I was wondering if it was possible to make it go to full hearts, THEN HALF hearts, before finally going to empty hearts. I already changed the heart value based on another comment, I just don't know how to change the sprites correctly between the three. (Great tutorial btw)
Thanks! I'm glad your finding the series helpful. The way the code is currently written, it wont naturally work for half sprites (since you would need to enable different sprites depending on whether it is a half or whole heart). That said, you could alter the code, and instead of changing the sprite from an empty to a full one based on your health, you could make all of your sprites half hearts, put them into your heart array, and then just enable or disable them based on health. In that case, your code would look something like: for(int i = 0; i < hearts.length; i++) { if ( i < health) {hearts[i].sprite.enabled = true;} else {hearts[i].sprite.enabled = false;} } That way you could load the left side of the heart sprite into slot 1 on the array, and the right side in slot 2 and it would enable/disable based on your health. I hope tha helps!
How do I make it so that the player would only lose a heart if it receives a certain amount of damage? Like if I have 100 player health and if I take 20 damage I would lost a heart, but not if I only took 15. Thanks for the tutorial btw.
I think the easiest way to do this would be to decide how much health each heart is worth in your game, then multiply you "i" (integer) by that number. So, if each heart is worth 15 health...instead of: if(i < health) you could write: if(i * 15 < health) though I would recommend create a variable so that you could change this value as your player levels. I would call this "heartValue" or something like that. Then you could write: if(i * heartValue < health) I hope that helps!
okay I cannot for the life of me drag anything at all into those heart slots (the amount). nothing shows up under select images either. triple checked steps carefully :(
Are your empty and full heart slots working? Or are all three not letting you drag stuff in? If all of them are the problem, did you remember to type "using UnityEngine.UI;" at the top of your script? If it's just the array that is the problem, did you make sure to add your hearts to the hierarchy as UI --> Image? (If they are normal sprites, they wont work). Hope one of those helps!
Hi! I'm using your heart system and i was wandering how can i make it so when player picks up an item he will get one additional heart? Do i have to add to max health or do i need to do an additional step? Thank you!
The system isn't inherently designed to do this, but you definitely could add that functionality. What you would want to do is: 1) Add all of the hearts that a player could possibly have right from the start. 2) In your Start() method, create a for loop that iterates through all of the empty hearts, turning them on as long as their length is not greater than your maxHealth. 3) The rest of your code would work as normal. 4) Now, whenever you gain maxHP you would want to call the same method you did in Start() so that it can turn on new empty hearts. It's hard to get it across without another tutorial, but hopefully that gets you off on the right foot.
Is there a way for me to reference a separate game object instead of a sprite renderer? I have a game object that has a completely functional restart button, but I want it to be disabled and only enable when Health reaches 0, as opposed to the sprites that only get disabled when Health reaches 0. I know that instead of “.enabled = false” I would just use “.enabled = true” but I don’t know how to actually select the game object I want nor how to select any game object at all. If this is worded weirdly let me know and I’ll try to word it better, but even if you don’t see this thank you so much for these videos, I seriously can’t find anything that even comes close to the simplicity yet effectiveness of these that doesn’t require any prior knowledge in programming 😄😄
You can reference a game object the same way you do with the sprite renderer. To turn it on or off though you have to use SetActive(true) instead of enabled. Enabled is for components, while setActive works for game objects. Once you make the reference to the GameObject at the top of your script, you can either drag your object into the new box in the inspector, or you can find it in your script by placing GameObject.Find(“your object name”) In your start function. Hope that helps!
for some reason when i add the image for some reason the canvas gets really big and i cant do anything about it what should i do? love your tutorials btw
Hmmn.... I'm not totally sure what you mean. The Canvas should always be the same size (it's not editable, but is always ginormous). I always like to put my scene and game view side-by-side, that way I can move the images around in the scene view, and see what it will look like in the game in my game view. Not sure if that addresses the problem, or if it's something else.
thank you for the tutorial! i was wondering is there a way for a heart animation to be played when the character is hit?i already have the animation i just dont know when to play it.
You can definitely do this, though it will help if you are pretty comfortable with Unity's animator already. 1) In your animator, create a LoseHeart animation (or whatever you want to call it), but make sure that it has a bool condition (maybe call it "lostAHeart"). Make sure that the only transition out of this animation has a condition that includes lostAHeart being false. 2) In your healthDisplay script, make sure that you have a reference to your animator up at the top: public Animator heartAnim; 3) Now, right above the emptyHeart line in your for loop, set your animator bool: heartAnim.SetBool("lostAHeart", true); 4) I think you can get rid of the emptyHeart line altogether. As long as your animation ends with an emptysprite and it is NOT on loop, it should just keep playing the empty heart sprite until your health gets restored. 5) Don't forget to hook up your animator in the Inspector in Unity. That should get the heart animation playing, and your heart should stay empty until your health is restored. You will probably want to do the opposite when gaining health (create a health gain animation that plays if lostAHeart is false. If you run into trouble implementing, feel free to pop into my discord server and post some code. Either myself or one of the members can probably help out. Cheers! discord.gg/G3eQqaCYq
Got a bit confused when your Health Display script suddenly changed to Heart Display script at 8:40 and the order of the Hearts list was different. (beneath maxhealth) after that, both health numbers dropped with damage but the hearts stayed the same.
So sorry about that! This was one of my earliest tutorials, and I sometimes missed stuff like that (I had to change the name due to a conflict in my own scripts, but should have noted it in the video so as not to confuse people. Thanks for pointing this out.
@@NightRunStudio I thought that might be the case. It's a good clear tutorial and I did get it working. I had multiple Players that collectively contributed to the health bar so it took a bit of working out, but it all works great. I'm from a modelling/animation background. Full game coding is a new venture for me (I normally bring code devs in but I wanted to understand the principals) This has become my go to channel and I'm really enjoying it.
@@DextraVisual That's awesome to hear! I love when animators and musicians and other creatives take the time to understand the code; I definitely think it broadens your overall understanding of the whole development process. Thanks for sharing, and good luck with your project!
Bro you should make a boss system in unity with powers to the boss like spawning enemies while fighting teleportation n if the boss dies the boss will blast etc. these are my suggestions overall big thanks
Hey man great video, however my player does not have a sprite renderer instead i just use a GameObject & Mesh renderer for him. Is it possible for me to do the same thing you did in the video without a sprie renderer? I could really use your help
I so wish I could help, but I’ve only ever worked with 2D, so I’m really not sure how this would map onto that. The basic structure would work for sure, but I’m not sure what you would want to do instead of changing sprites. Sorry about that!
@@NightRunStudio nevermind i fixed it by instead of writing Spriterenderer i wrote Meshrenderer and also Trailrenderer since my player has a trail following him. Thanks for the tutorial man. Keep up the good work
I'm afraid I don't have a Github or place to download resources at this point--hopefully as the channel grows I can add this. That said, openGameArt has some great stuff that might be helpful: opengameart.org/art-search?keys=heart
I'm using this in a 3D game, the mesh renderers would still show the last heart when the player dies, so I changed it to disable the player's game object and it is still showing the last 2 hearts when the player dies. Any solutions for this?
It sounds like you may be disabling the player before your script runs the update on your heart UI. Did you try the suggestion at the very end (around 10:30) for disabling sprite renderer and movement instead of disabling the player completely? That way your health script can finish doing it's thing, but it will look like you died.
Unfortunately, those are just images I created for my own stuff. That said, there is a lot of great free art available out there. For example, OpenGameArt has some great stuff. Here's one link if you're interested, but if you don't love this one there's lots more out the to find: opengameart.org/content/simple-small-pixel-hearts
Love this tutorial however, I have a problem with my sprite right at the very end where the script doesn't disable the sprite but it does disable the movement. Where have I gone wrong do you think?
Sounds like there may be a problem with your SpriteRenderer reference. Are you getting any errors in your console? If there are no errors, you might need to use a debug line to try to find what is causing the problem. If you aren't familiar with doing this, you could try adding something like: Debug.Log(playerSr.gameObject.name); If you put that line right before the playerSr.enabled line it should print the name of the object that has the sprite renderer in your console. If it is your player's name, then you know your sprite renderer reference is good, if it's something else, then you know this is you problem. Hopefully that helps. Let me know if you have any other questions.
@@NightRunStudio I have not got any errors when it plays. Cool, I will give that a go. I do have in Sprite renderer a box at the top that says Sprite and in the box "None (Sprite)" is that something that could be causing the problem?
@@hanpolo2727 That seems weird that your sprite renderer box is empty--if it's empty, you shouldn't be able to see any sprite at all. Is it possible that you have multiple sprite renderers on this object (maybe in the object's children)? If so, it could be that you are disabling the sprite renderer that has no sprite, but allowing another one to continue to shows its sprite.
@@NightRunStudio Oh wait a moment I may have it. My gaming teacher taught us when making the player, to have all scripts and RigidBody2D in a parent then animator and capsule collider 2D and then sprite renderer in a child. I also have a second child called Ground Check too.
@@hanpolo2727 That could definitely be the problem--and that's good advice from your teacher! If you put sprites and physics on the same object, all kinds of weird stuff can happen.
Thank you so much! This has helped me very much. I've been trying to make a health system multiple times, but i failed miserably. Great tutorial!❤
Thanks for sharing! I’m so glad it helped.
Amazing tutorial! Just did it and works perfectly
Awesome! I love hearing that. Good luck with the rest of your project!
Thank you so much for this great tutorial! I wouldn't have figured this out alone lol
So glad to hear it helped! Thanks for sharing.
yo thanks so much this really helped me out!!
Awesome! So glad to hear it was helpful.
these tuts are amazing
Awww… thanks! Glad they are helpful!
i cant find any goot sprites for it do you have a link to the download of these sprites?
Great vid btw deserves way more attention
Nvm found one. I got a script where if i die i get respawned via prefabs. But if i do all the hearts in the [ ] array are gone. Do you know how i could fix that?
@@Fwsr. Glad to hear that you found some graphics assets!
As for the problem you describe, this is definitely happening because your prefab doesn't know what heart images it is supposed to interact with. I can think of two main ways to fix this:
1) Don't use a prefab for your character. Instead of spawning a new character, just teleport the existing one back to a spawn point and set its health back to full. (This would be the easiest fix).
2) Since you can't drag your heart images into your prefab's script, you will need to code it. You can do this in your Start() function (so that it happens as soon as your character Instantiates into the scene). It would look something like:
Hearts[1] = GameObject.Find(Heart1).GetComponent();
Hearts[1] refers to the array, so you'll need to do this for all of the hearts on your player.
(Heart1) refers to the name of the game object (in your hierarchy) that you are trying to reference.
I hope this helps! Good luck with your project.
Great Vid ! Just wondering if my player change scene is this still functionnal ?
It will work, but as with any scene change stuff, you have to decide how you want it to persist. For example, I would use DontDestroyOnLoad on both the UI and the player so that they persist when you scene change.
@@NightRunStudio Oh yes thank you very much ! I click subscribe your so fast to response it's amazing (and sorry english is not my first language)
I got the whole script and everything to work, but I was wondering if it was possible to make it go to full hearts, THEN HALF hearts, before finally going to empty hearts. I already changed the heart value based on another comment, I just don't know how to change the sprites correctly between the three. (Great tutorial btw)
Thanks! I'm glad your finding the series helpful.
The way the code is currently written, it wont naturally work for half sprites (since you would need to enable different sprites depending on whether it is a half or whole heart). That said, you could alter the code, and instead of changing the sprite from an empty to a full one based on your health, you could make all of your sprites half hearts, put them into your heart array, and then just enable or disable them based on health.
In that case, your code would look something like:
for(int i = 0; i < hearts.length; i++)
{ if ( i < health)
{hearts[i].sprite.enabled = true;}
else
{hearts[i].sprite.enabled = false;}
}
That way you could load the left side of the heart sprite into slot 1 on the array, and the right side in slot 2 and it would enable/disable based on your health.
I hope tha helps!
@@NightRunStudio I got it to work. Thank you very much!
@@abaier82 Awesome! Congrats on figuring out how to adapt it for your project. I always find that the most rewarding!
Cool I really needed this
Awesome! Glad it helped.
How do I make it so that the player would only lose a heart if it receives a certain amount of damage? Like if I have 100 player health and if I take 20 damage I would lost a heart, but not if I only took 15. Thanks for the tutorial btw.
I think the easiest way to do this would be to decide how much health each heart is worth in your game, then multiply you "i" (integer) by that number. So, if each heart is worth 15 health...instead of:
if(i < health)
you could write:
if(i * 15 < health)
though I would recommend create a variable so that you could change this value as your player levels. I would call this "heartValue" or something like that. Then you could write:
if(i * heartValue < health)
I hope that helps!
@@NightRunStudio thanks so much!
this is really helpfull, btw can i request a tutorial?, respawn tutorial pls
Thanks for the feedback. Respawn would be a great tut. I’ll look into making one.
@@NightRunStudio i will wait for it
okay I cannot for the life of me drag anything at all into those heart slots (the amount). nothing shows up under select images either. triple checked steps carefully :(
Are your empty and full heart slots working? Or are all three not letting you drag stuff in? If all of them are the problem, did you remember to type "using UnityEngine.UI;" at the top of your script? If it's just the array that is the problem, did you make sure to add your hearts to the hierarchy as UI --> Image? (If they are normal sprites, they wont work). Hope one of those helps!
@@NightRunStudio thanks much NRS. I have all those yes but I will keep searching it will work eventaully. thanks!
Useful video man just learn utube algorithm to reach more people
Thanks for that. I obviously have some research to do :)
Hi! I'm using your heart system and i was wandering how can i make it so when player picks up an item he will get one additional heart? Do i have to add to max health or do i need to do an additional step? Thank you!
The system isn't inherently designed to do this, but you definitely could add that functionality. What you would want to do is:
1) Add all of the hearts that a player could possibly have right from the start.
2) In your Start() method, create a for loop that iterates through all of the empty hearts, turning them on as long as their length is not greater than your maxHealth.
3) The rest of your code would work as normal.
4) Now, whenever you gain maxHP you would want to call the same method you did in Start() so that it can turn on new empty hearts.
It's hard to get it across without another tutorial, but hopefully that gets you off on the right foot.
woah coding class
Is there a way for me to reference a separate game object instead of a sprite renderer? I have a game object that has a completely functional restart button, but I want it to be disabled and only enable when Health reaches 0, as opposed to the sprites that only get disabled when Health reaches 0. I know that instead of “.enabled = false” I would just use “.enabled = true” but I don’t know how to actually select the game object I want nor how to select any game object at all. If this is worded weirdly let me know and I’ll try to word it better, but even if you don’t see this thank you so much for these videos, I seriously can’t find anything that even comes close to the simplicity yet effectiveness of these that doesn’t require any prior knowledge in programming 😄😄
You can reference a game object the same way you do with the sprite renderer. To turn it on or off though you have to use SetActive(true) instead of enabled. Enabled is for components, while setActive works for game objects.
Once you make the reference to the GameObject at the top of your script, you can either drag your object into the new box in the inspector, or you can find it in your script by placing
GameObject.Find(“your object name”)
In your start function.
Hope that helps!
for some reason when i add the image for some reason the canvas gets really big and i cant do anything about it what should i do?
love your tutorials btw
Hmmn.... I'm not totally sure what you mean. The Canvas should always be the same size (it's not editable, but is always ginormous). I always like to put my scene and game view side-by-side, that way I can move the images around in the scene view, and see what it will look like in the game in my game view. Not sure if that addresses the problem, or if it's something else.
thank you so much
Half it was helpful!
thank you for the tutorial! i was wondering is there a way for a heart animation to be played when the character is hit?i already have the animation i just dont know when to play it.
You can definitely do this, though it will help if you are pretty comfortable with Unity's animator already.
1) In your animator, create a LoseHeart animation (or whatever you want to call it), but make sure that it has a bool condition (maybe call it "lostAHeart"). Make sure that the only transition out of this animation has a condition that includes lostAHeart being false.
2) In your healthDisplay script, make sure that you have a reference to your animator up at the top:
public Animator heartAnim;
3) Now, right above the emptyHeart line in your for loop, set your animator bool:
heartAnim.SetBool("lostAHeart", true);
4) I think you can get rid of the emptyHeart line altogether. As long as your animation ends with an emptysprite and it is NOT on loop, it should just keep playing the empty heart sprite until your health gets restored.
5) Don't forget to hook up your animator in the Inspector in Unity.
That should get the heart animation playing, and your heart should stay empty until your health is restored. You will probably want to do the opposite when gaining health (create a health gain animation that plays if lostAHeart is false.
If you run into trouble implementing, feel free to pop into my discord server and post some code. Either myself or one of the members can probably help out. Cheers! discord.gg/G3eQqaCYq
Thanks!
My pleasure!
Got a bit confused when your Health Display script suddenly changed to Heart Display script at 8:40 and the order of the Hearts list was different. (beneath maxhealth) after that, both health numbers dropped with damage but the hearts stayed the same.
So sorry about that! This was one of my earliest tutorials, and I sometimes missed stuff like that (I had to change the name due to a conflict in my own scripts, but should have noted it in the video so as not to confuse people. Thanks for pointing this out.
@@NightRunStudio I thought that might be the case. It's a good clear tutorial and I did get it working. I had multiple Players that collectively contributed to the health bar so it took a bit of working out, but it all works great.
I'm from a modelling/animation background. Full game coding is a new venture for me (I normally bring code devs in but I wanted to understand the principals) This has become my go to channel and I'm really enjoying it.
@@DextraVisual That's awesome to hear! I love when animators and musicians and other creatives take the time to understand the code; I definitely think it broadens your overall understanding of the whole development process.
Thanks for sharing, and good luck with your project!
Bro you should make a boss system in unity with powers to the boss like spawning enemies while fighting teleportation n if the boss dies the boss will blast etc. these are my suggestions overall big thanks
Thanks! I am hoping to do an Enemy AI series soon which will hopefully include a boss fight--so great ideas! Thanks for sharing.
Hey man great video, however my player does not have a sprite renderer instead i just use a GameObject & Mesh renderer for him. Is it possible for me to do the same thing you did in the video without a sprie renderer? I could really use your help
I so wish I could help, but I’ve only ever worked with 2D, so I’m really not sure how this would map onto that. The basic structure would work for sure, but I’m not sure what you would want to do instead of changing sprites. Sorry about that!
@@NightRunStudio nevermind i fixed it by instead of writing Spriterenderer i wrote Meshrenderer and also Trailrenderer since my player has a trail following him. Thanks for the tutorial man. Keep up the good work
Hello, I am unable to drag and drop the hearts into the list, do you have any ideas as to why?
Are the hearts UI images, or did your drag them in as sprites? Sometimes people just put them in as sprites, and then the array wont recognize them.
I can't seem to find the empty heart sprite, can you give me the link to it? Thanks
I'm afraid I don't have a Github or place to download resources at this point--hopefully as the channel grows I can add this. That said, openGameArt has some great stuff that might be helpful:
opengameart.org/art-search?keys=heart
I'm using this in a 3D game, the mesh renderers would still show the last heart when the player dies, so I changed it to disable the player's game object and it is still showing the last 2 hearts when the player dies. Any solutions for this?
It sounds like you may be disabling the player before your script runs the update on your heart UI. Did you try the suggestion at the very end (around 10:30) for disabling sprite renderer and movement instead of disabling the player completely? That way your health script can finish doing it's thing, but it will look like you died.
Bro can you give the both hearts link? pls
Unfortunately, those are just images I created for my own stuff. That said, there is a lot of great free art available out there. For example, OpenGameArt has some great stuff. Here's one link if you're interested, but if you don't love this one there's lots more out the to find: opengameart.org/content/simple-small-pixel-hearts
How is it possible that he only looses 1 heart instead of 2?
Did you set it to lost 2?
Love this tutorial however, I have a problem with my sprite right at the very end where the script doesn't disable the sprite but it does disable the movement. Where have I gone wrong do you think?
Sounds like there may be a problem with your SpriteRenderer reference. Are you getting any errors in your console?
If there are no errors, you might need to use a debug line to try to find what is causing the problem. If you aren't familiar with doing this,
you could try adding something like:
Debug.Log(playerSr.gameObject.name);
If you put that line right before the playerSr.enabled line it should print the name of the object that has the sprite renderer in your console. If it is your player's name, then you know your sprite renderer reference is good, if it's something else, then you know this is you problem.
Hopefully that helps. Let me know if you have any other questions.
@@NightRunStudio I have not got any errors when it plays.
Cool, I will give that a go. I do have in Sprite renderer a box at the top that says Sprite and in the box "None (Sprite)" is that something that could be causing the problem?
@@hanpolo2727 That seems weird that your sprite renderer box is empty--if it's empty, you shouldn't be able to see any sprite at all. Is it possible that you have multiple sprite renderers on this object (maybe in the object's children)? If so, it could be that you are disabling the sprite renderer that has no sprite, but allowing another one to continue to shows its sprite.
@@NightRunStudio Oh wait a moment I may have it. My gaming teacher taught us when making the player, to have all scripts and RigidBody2D in a parent then animator and capsule collider 2D and then sprite renderer in a child. I also have a second child called Ground Check too.
@@hanpolo2727 That could definitely be the problem--and that's good advice from your teacher! If you put sprites and physics on the same object, all kinds of weird stuff can happen.
Thank you so much it worked😭😭😭😭😭 making my dreams come true 🥲
Hahaha! So glad it worked.
I felt this 😢😂 I will be trying this tutorial soon