How to create a simple UI health bar for a character in Unity game. Easy tutorial.

hello everyone in this video I will show you how to create a simple health bar for your unity game first let's create new UI image game object and put health bar background sprite to the source image slot of its image script component double click on image to get a focus then check preserve aspect option position our image at the top center holding alt shift keys make our image bigger by changing its weak I rename this game object as health bar background then right-click on health bar background and create new UI image name it health bar put healthbar sprite to source the image slot of image script check preserve aspect option and change health bar width so it's a bit smaller than a backgrounds one and curl it to the top center then focus on it and correct its position then go to image script component and change image type from simple to field after that change fuel method to horizontal now if we direct the field amount slider then we can see how our image changes we're gonna change fill amount value through the script okay get back to the scene view let's add a player and the damage zone at rigidbody2d component to the player with gravity scale set to zero and collision detection set to continuously then add a circle Collider edit it to fit the shape of the sprite then add a circle Collider to the skull and check is trigger option time to add some scripts let's take a look at health bar script first here we have image health bar game object float max health variable which equals 200 and public static float health variable in stock method we get health bar image component and set health variable 200 and in update method we simply set a fill amount to health value divided by max health put this drip to health bar game object next let's take a look at the move script for our player it is simple movement script that allows us to move our player with a rigidbody component attached by pressing arrow keys at this trip to our player the Scout script is the easiest one here we have only one method which is ontriggerenter TDD so if something collides with the skull then health variables value of health bar script is decreased by 10 put this trip to the scout game object everything is done let's hit play and see how it works player collides with the skull and health is decreased thank you for watching see you next time


  1. Hi everybody! Please share this video if you like it! I will be very appreciated for that! Thanks for watching)

  2. It works ! Little tweaks and it worked for 3D /Android Project too

    **********************Skull Script********************
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class skull : MonoBehaviour
    void OnTriggerEnter(Collider other)
    { -= 10f;


    ***************** Health Bar Script ******************
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class healthBar : MonoBehaviour
    Image healthBari;
    float maxHealth = 100f;
    public static float health;

    // Start is called before the first frame update
    void Start()
    healthBari = GetComponent<Image>();
    health = maxHealth;

    // Update is called once per frame
    void Update()
    healthBari.fillAmount = health / maxHealth;

  3. Quick and easy even for a dumb doll, very well done, in fact i spent more time writing this comment here than actually understanding what the tutorial is about.

  4. thanks for the video. Finally an explanation thats easy, fast and actually 100% works (unlike 99% of the other vids about this topic).

  5. You can actually create your background image, and then create a child image of what it would look like empty. Let's say you create a dark red back ground with a black empty image. you make the black empty image your backgrounds child object. (Set native size and resize each time). Then you want a green image for your full bar, which will be your full health bar. (For example)
    Give your black empty image a component called Mask. Then make your full bar a child of that. You wont need to do the resizing that you see here. Not taking anything from Alexander Zotov because the man is a genuis! I'm offering an alternative that I find to be much more efficient.

  6. I made the UI image but I have two rookie problems: first, the image shows on the scene tab but not the game tab; and second, I can't get the image to show in front of my other sprites, its always behind them so my health bar is blocked. Can someone help please?

  7. thanks a lot, i saw a 25 minutes video that actually said nothing and you solve my problem in only a few minutes, thanks

  8. is worck but i have some questions
    1. if have 2 objects with the same code haw to add damage only in the specific objects ?
    has 2 players and 1 trigger collider that makes 10 damage haw to add for the trigger collider to do only in the enter player ?
    2. why not maxHealth is not public ?

  9. hi alexander can you make a xp bar tutorial. i always like your tutorial because they are simple, straighrt forward

Leave a Reply

(*) Required, Your email will not be published