Transcript for:
Creating a Simple Health Bar in Unity

in this unity tutorial i'll be showing you how to create a simple health bar in unity create a canvas set the scale mode to scale with screen size and set the resolution to 1920x1080 create a ui image as a child of the canvas change its sprite to a square change the color to black and resize it to best suit your game rename this to border and duplicate it change its color to red and slightly downsize it then duplicate the red image and set the color to green on the green image change the image type to field fill method to horizontal and the fill origin to left create an empty game object and call it health manager then create a new c sharp script called health manager and drag it onto the newly created game object now in the health manager script include using unity engine.ui up the top create public variables for the health by image and a float for the health amount create a function called take damage passing in a float argument called damage in the function subtract the damage amount from the health value and set the health bar fill amount to the health amount divided by your max health next up create a heal function passing in a healing amount argument add the healing amount to the health then clamp the health between zero and your max health then set the health bar fill amount like we just did in update check if the player is pressing the enter key if they are call the take damage function passing in a damage amount to deal to the player create another input check to see if the player is pressing the space bar this time call the heal function passing in an amount we want to heal the player make one more if statement checking if the player's health is less than or equal to zero if that's the case we're going to reload the scene back in unity drag your green health by image into the health manager script and press play by using the enter key and space bar you can now deal damage to the player and watch the health bar update and that's a simple way to make a quick health bar in unity if there are any other tutorials you'd like to see from me let me know in the comments below thanks for watching and i'll see you in the next one