HEALTH BAR Unity UI Tutorial (Beginner Friendly)

Поділитися
Вставка
  • Опубліковано 20 жов 2024

КОМЕНТАРІ • 124

  • @BMoDev
    @BMoDev  4 роки тому +16

    It may not look stunning, but you can create a working health bar in less than 5 minutes! Not bad

    • @Mccev_Roric
      @Mccev_Roric 4 місяці тому

      I don't know if you will see this, but when I change the current health, the fill goes to either 1 or 0 instead on .9, .8, .7 ect. here is the code for the fill bar status:
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      using UnityEngine.UI;
      public class FillStatusBar : MonoBehaviour
      {
      public PlayerHealth playerHealth;
      public Image fillImage;
      private Slider slider;
      void Awake()
      {
      slider = GetComponent ();
      }
      void Update()
      {
      float fillValue = playerHealth.currentHealth / playerHealth.maxHealth;
      slider.value = fillValue;
      }
      }
      I also have the PlayerHealth script:
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class PlayerHealth : MonoBehaviour
      {
      public int currentHealth;
      public int maxHealth;
      }
      I have been stuck on this for a while now, so if you could help that would be great.

  • @Abraham-om5ib
    @Abraham-om5ib 4 роки тому +8

    wow I actually understood this as a beginner, thank you :)

  • @aadilgoyal4198
    @aadilgoyal4198 4 роки тому +1

    This video helped alot. I was searching literally for 2hrs. Thank you for making this video

  • @msingles61
    @msingles61 2 роки тому

    You're a legend mate, your entire channel makes bringing our ideas to life so easy even when people are just starting out. Keep it up ❤️

  • @yasmina7194
    @yasmina7194 Місяць тому

    Plz do more of the 5 minute videos.
    Love your work!!!

  • @moopdoop9942
    @moopdoop9942 4 роки тому +6

    i made the player script with everything right, but forgot to make it a float. i spent way too long wondering why it wasnt working. Thanks for the video!

    • @hhhhh13
      @hhhhh13 Рік тому

      HAHAHAHHAHH SAMEEEEE

  • @GamesHub7
    @GamesHub7 2 роки тому

    I come here now and then just for the fun part in the beginning!

  • @ThunderPony
    @ThunderPony 5 місяців тому

    I'm watching the whole playlist and loving it for the simplicity!
    Could you explain more about the awake() function and when to use that vs the start() method?

  • @clerklyant2983
    @clerklyant2983 2 роки тому

    this was the only tutorial that worked for me

  • @TheFibrewire
    @TheFibrewire 3 роки тому

    WOW this seems to be more better than using SetMax and Health methods.

  • @luyolombatha414
    @luyolombatha414 3 роки тому +1

    thanks for this, love you brother man

  • @jakeduncan2721
    @jakeduncan2721 4 роки тому +3

    11/10 however I demand more subscribe party at the end of this video. perhaps a swarm of knight subscribers to storm the castle?
    Thanks bmo,
    Jake

    • @BMoDev
      @BMoDev  4 роки тому

      I'll make it up to you in a future health bar video :)

  • @aaronm9576
    @aaronm9576 3 роки тому +4

    I followed your both tutorials to what you exactly done but when i adjust the health of the player the health bar goes instantly to 0? unsure why

    • @BMoDev
      @BMoDev  3 роки тому +8

      In your health script try switching from ints to floats

    • @aaronm9576
      @aaronm9576 3 роки тому +1

      @@BMoDev I spent a long time trying to figure it out. I followed your first health video which declared them as Ints first . I wasnt aware that they were supposed to be floats in your follow up video, I found the answer in one of the comments. I appreciate the reply, it has been fixed although when I change the current health value to no number and then change it back to a valid number the healthbar does not go back to the correct position

    • @gwendal5153
      @gwendal5153 3 роки тому +1

      @@BMoDev it worked thanks

  • @ensjsjjfhdhdudh6435
    @ensjsjjfhdhdudh6435 4 роки тому

    Finally becoase you made this tutorial i can continue my game project Thank you!!!

    • @BMoDev
      @BMoDev  4 роки тому

      Thats awesome, good luck!

  • @thecanaeggplant2937
    @thecanaeggplant2937 4 роки тому +1

    İ cant find the player health script, and idont see it on the video

  • @ilgaza615
    @ilgaza615 4 роки тому +4

    whats the script for the player health?

  • @aleksandraaleksandra7934
    @aleksandraaleksandra7934 Рік тому

    Thanks for a great, simple tutorial. I'm making a game and having many different mechanics, scripts, objects, I'm looking for simple solutions to combine everything into one. You did it great!

  • @frankaors5223
    @frankaors5223 3 роки тому +1

    hey bmo do you know why when clicking play my bar dissapears? exactly talking abt min 3:06 (my game is set on the bottom left corner and the slider too

  • @EvaseWingmanYT
    @EvaseWingmanYT Рік тому

    I have a question but I have an object in my game like a floor spike which is supposed to have my player's health bar degenerate a bit of health, do i need to add an onTriggerEnter2D next to it because the floor spikes have the box collider 2D?

  • @luffyakai6757
    @luffyakai6757 Місяць тому

    is the player health script the same health script as the other video?

  • @jesseostrander2939
    @jesseostrander2939 3 роки тому +2

    thanks for the tutorial, currently trying to use an Image like a face to indicate the remaining hp. but for some reason i am unable to get it working with the hp slider. the slider updates fine but the image doesnt change when hp is < MAXHP. if i could get your help id be most thankful

  • @aleksandraaleksandra7934
    @aleksandraaleksandra7934 Рік тому

    Can someone explain how I can add here that life is increased by taking a boost? The video from this channel on this topic did not help me, and I'm tired for another hour and still something is wrong.
    I'm trying to do something like:
    if the player touches an object with a booster tag, slider +1. But there is a mistake somewhere in my thinking.

  • @Zaddy.Griff.
    @Zaddy.Griff. 9 місяців тому +1

    im gettting error code cs0246 on the line that says "public PlayerHealth playerHealth" anyone know how to fix this?
    hoping i get a fast reply cause this was 4 years ago

    • @namk0163
      @namk0163 6 місяців тому

      Do you have another script called 'PlayerHealth' like the guy in the video? If you have a script for your player's health with a different name , you may get an error.

  • @rat1155
    @rat1155 3 роки тому

    Great video!

  • @robistefan29
    @robistefan29 4 роки тому +2

    how do i make something damgage the player? i watch all the comments tried the solutions you gave them and did not work :(

    • @BaconJeezbrgr
      @BaconJeezbrgr 3 роки тому +3

      public float health = 100f;
      public float damage = 25f;
      void OnCollisionEnter2D(Collision2D other)
      {
      if (other.gameObject.CompareTag("Enemy")
      {
      health -= damage;
      if (health

    • @UNknown-xk1hh
      @UNknown-xk1hh 3 роки тому +3

      @@BaconJeezbrgr Thanks a lot!
      I was hoping for this code for a looong time and i finally got it!

    • @yoyo12345
      @yoyo12345 3 роки тому +1

      @@UNknown-xk1hh if u are using bullets to hurt the player then u must use a line cast to check the collision

    • @UNknown-xk1hh
      @UNknown-xk1hh 3 роки тому +1

      @@yoyo12345 Thanks

    • @yoyo12345
      @yoyo12345 3 роки тому +1

      @@UNknown-xk1hh ur comment was 6 months old!?! Didn't expected that u will reply
      Btw what's Ur unity progress?

  • @ahmed-0775
    @ahmed-0775 Рік тому +1

    Thanks alot!

  • @tombusckull
    @tombusckull 4 роки тому

    when i do ctrl + . the same things dont pop up and i dont have the max health and player health on my player

  • @GamesHub7
    @GamesHub7 2 роки тому

    Come on how can this video be underrated, maybe gamedevs don't like jokes?
    (If you have seen a Unity's video where two people try to joke while teaching, gets the same reaction, I feel like gamedevs don't like jokes?)

  • @jamestran376
    @jamestran376 3 роки тому

    how do you make a health bar that goes down if you collide with something? I'm trying to make this game and I just cant find a way to make my player lose health if it collides with an enemy object. Plz help

  • @hasanabdalla7102
    @hasanabdalla7102 2 роки тому +1

    I am getting a null reference for the float fillValue please help

  • @azizasuleymen7338
    @azizasuleymen7338 3 роки тому +1

    Where i can take PlayerHealth script?

  • @dominikkatavic5060
    @dominikkatavic5060 4 роки тому

    I know this is not theme of the video but how can i make player lose health when he is colliding with something

  • @treblanitram6505
    @treblanitram6505 Рік тому

    I'm having a problem, Object Reference not set to an instance of an Object. Send help pls😢.

    • @treblanitram6505
      @treblanitram6505 Рік тому

      Update: Already fixed it, just a bug from Unity I guess, the script was applied to multiple objects dunno why.

  • @ryan123k4
    @ryan123k4 3 роки тому +4

    pov: Your looking for the script

  • @drama.dote_
    @drama.dote_ 4 роки тому

    thanks. it is really helpful.

    • @BMoDev
      @BMoDev  4 роки тому

      Cool, I'm glad :)

  • @Asdzxc120
    @Asdzxc120 4 роки тому

    how did you create the currenthealth?

  • @gravitymilk5251
    @gravitymilk5251 4 роки тому +1

    good video thanks

  • @LION303200
    @LION303200 4 роки тому

    put the castle is still attacking you even after you died? how to stop them attack when u die?

    • @BMoDev
      @BMoDev  4 роки тому

      Well in this video its hardcoded.. the short answer would be to check if your player health is equal to 0 and then stop your enemy AI from attacking the player... maybe re-enter patrol mode or something.

  • @mervenurfem8842
    @mervenurfem8842 3 роки тому

    Thanks!

  • @Asdzxc120
    @Asdzxc120 4 роки тому

    does this video has a next video? i mean how can we reduce the current health bar

    • @BaconJeezbrgr
      @BaconJeezbrgr 3 роки тому

      float fillValue = PlayerHealt.CurrentHealth / PlayerHealth.MaxHealth;

  • @lyuboslavnemtsov8417
    @lyuboslavnemtsov8417 4 роки тому +1

    It is great indeed,however
    if my currenthealth < maxHealth slider is min value
    currenthealth > maxHealth slider is max value
    how can i fix this

    • @DerCriticer
      @DerCriticer 4 роки тому +4

      It's probably because you are using int variables for your maxHealth and currentHealth. Integer can't store decimal values, they will automatically be rounded down. So 60/100 would equal 0 instead of 0.6. Try using float instead.

    • @lyuboslavnemtsov8417
      @lyuboslavnemtsov8417 4 роки тому

      Thanks!

    • @rljeric
      @rljeric Рік тому +1

      @@DerCriticer your solution fixed it for me, but in his tutorial he used integers and it worked. how does that make sense? I followed it exactly like he said and i got the same issue that Lyuboslav had

  • @adrianfelix4518
    @adrianfelix4518 2 роки тому

    I'm not sure if you'll see this, but hopefully you do. But I'm having a bug where the slider only works properly for max health and nothing else, do you know how to fix this?

    • @vladeko4258
      @vladeko4258 2 роки тому

      I figured out solution. When you have float fillValue, then delete / maxHealth and then in Unity set the max value of the slider to 10.

    • @adrianfelix4518
      @adrianfelix4518 2 роки тому

      @@vladeko4258 I just decided to go to a different tutorial by Night Run Studio that luckily worked

    • @heinkelion
      @heinkelion Рік тому +2

      It's as simple as putting (float) before the "playerHealth.currentHealth" and "playerHealth.maxHealth".

    • @MitchIcarus
      @MitchIcarus Рік тому +1

      @@heinkelion thank you soooo much. In my playerHealth script I used public int instead of public float and because of it as soon as my player took damage the health bar dropped to 0

    • @kookiesnmilk6950
      @kookiesnmilk6950 Рік тому

      @@heinkelion i love you. thank you

  • @thecanaeggplant2937
    @thecanaeggplant2937 4 роки тому +1

    where is the player health script ?

    • @krjamesjr
      @krjamesjr 4 роки тому +1

      I was looking for this, too! Looks like he answers this in SneakyTurtle's comment below from 3 months ago.

  • @Lokithh
    @Lokithh 4 роки тому +2

    using a slider made it much easier lol I wish you showed how the arrows were causing damage tho

    • @BMoDev
      @BMoDev  4 роки тому

      When starting out especially its great to utilize the tools Unity provides for us! I can definitely show how this is done, the quick overview is when the arrow collides with the player it makes a call of GetComponent() and subtracts 1 from the currentHealth!

  • @charliekennedy4216
    @charliekennedy4216 4 роки тому

    For some reason it's not letting me drag the Fill Image onto the FillStatusBar script (the script on the slider). Anyone know how to fix it?

    • @charliekennedy4216
      @charliekennedy4216 4 роки тому

      Nevermind, I found it. Brilliant tutorial!

    • @BMoDev
      @BMoDev  4 роки тому

      Sorry this took me a while to respond! Appreciate the kind words!

  • @GabeTheYabe
    @GabeTheYabe 4 роки тому

    So I got it to work, but I will like to get it to work whenever an enemy shoots me or collides into me, it reduces my health. I was thinking to add it in this script yet I was getting an error: nullRefrenceException object reference not set to an instance of an object.
    public class DestroyByContact : MonoBehaviour
    {
    public GameObject enemyExplosion;
    public GameObject playerExplosion;
    public PlayerHealth playerHealth;
    public int scoreValue;
    private GameController gameController;
    private void Start()
    {

    GameObject gameControllerObject = GameObject.FindWithTag("GameController");
    if (gameControllerObject != null)
    {
    gameController = gameControllerObject.GetComponent();
    }
    else
    {
    Debug.Log("Cannot find 'GameController' script");
    }
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
    // Name of the given object that collided with the enemy
    var name = other.tag;
    // if the name of the tag is the same as to Boundary tag
    // return and do nothing
    if (name == "Boundary" || name == "Enemy")
    {
    return;
    }
    if(enemyExplosion != null)
    {
    Instantiate(enemyExplosion, this.transform.position, this.transform.rotation);
    }
    if (name == "Player")
    {

    Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    gameController.GameOver();

    }
    gameController.IncrementScore(scoreValue);
    Destroy(other.gameObject);
    Destroy(this.gameObject);
    }
    }

  • @his_of_ham
    @his_of_ham Рік тому

    Doesn't work.
    Edit: Now works but I changed the equation using Math.Lerp to change the answer into a float

  • @whitekanyewestthebully1346
    @whitekanyewestthebully1346 3 роки тому

    What’s the payer script

  • @charliekennedy4216
    @charliekennedy4216 4 роки тому

    Hello, great tutorial! I'm trying to make it automatically take one away from the playerHealth when the player gets hit by an enemy, but it's not working as it's a float. Does anyone know how to get around this?

    • @BMoDev
      @BMoDev  4 роки тому +1

      You can certainly subtract 1 from a float.
      float myhealth;
      myhealth -= 1f;

    • @charliekennedy4216
      @charliekennedy4216 4 роки тому

      @@BMoDev Ohhhh, I'll try that again - thanks!

    • @charliekennedy4216
      @charliekennedy4216 4 роки тому

      @@BMoDev No, sadly it's not working: here's the error:
      error CS0019: Operator '-=' cannot be applied to operands of type 'PlayerHealth' and 'float'
      and here's the code:
      public int fillValue;
      public PlayerHealth playerHealth;
      public void OnTriggerEnter2D(Collider2D col)
      {
      if (col.gameObject.tag == "Hurt")
      {
      playerHealth -= 1f;
      }
      }
      }

    • @BMoDev
      @BMoDev  4 роки тому +2

      Ah I see, Ok
      So PlayerHealth is a class, you need to access the variable inside of the class.
      Not sure how your project is setup, but you can create a new script called PlayerHealth, and inside that just add two variables
      public class PlayerHealth: MonoBehaviour
      {
      public float currentHealth;
      public float maxHealth;
      }
      and attach it to the Player. If you click on your player, in this script you'll see these variables appear and you can set values to them.
      ----
      then, back in your HealthBar script, use playerHealth.currentHealth -= 1;
      let me know if you get it working!

    • @charliekennedy4216
      @charliekennedy4216 4 роки тому +3

      @@BMoDev Just got it working - thank you so much! Incredibly helpful tutorials and you put so much work into helping others, one of the most useful game dev tutorial accounts i know. Keep it up!!

  • @elijahbyrd3370
    @elijahbyrd3370 4 роки тому

    Hmm my issue is that it goes straight to 0 my health script gives the player 10 hp but it just goes straight to 1

    • @BMoDev
      @BMoDev  4 роки тому +2

      Are you using integers instead of floats? My first guess is the integers (which won't allow decimals) is messing up the math. If you are, try using floats

    • @lewishinshelwood123
      @lewishinshelwood123 2 роки тому

      @@BMoDev This worked change your current and max health to a float😌

  • @mayadgaf
    @mayadgaf 4 роки тому

    New subscriber here! I really like your videos but when I'm watching you script I can't really see what you're typing mainly because it's to small

    • @BMoDev
      @BMoDev  4 роки тому +2

      ayyy thanks for the support! This was something brought up so in my more recent videos I have the font zoomed in on, it should be better going forward :)

  • @gujeshwarideviupadhyay9875
    @gujeshwarideviupadhyay9875 3 роки тому

    Bro can you make a video for 2020 unity but good video bro keep it up

  • @SpotechYT
    @SpotechYT 4 роки тому

    whats the player script?

  • @justusisbestest4122
    @justusisbestest4122 2 роки тому

    TY TY TY

  • @khemziegamer4790
    @khemziegamer4790 4 роки тому

    U didnt show us the player health script

    • @BMoDev
      @BMoDev  4 роки тому

      PlayerHealth is a script attached to my Player GameObject. It just has two float variables within it :
      public float maxHealth;
      public float currentHealth;
      There's nothing really to it which is why I didn't show it, you'll have to create a health system for your project to work with the health bar.

    • @khemziegamer4790
      @khemziegamer4790 4 роки тому

      @@BMoDev thank you so much

  • @bsdev6894
    @bsdev6894 4 роки тому

    Why doesn't it show my fill image?

    • @BMoDev
      @BMoDev  4 роки тому

      Not sure what your setup is. The slider by default gives you a fill image that shows.

    • @bsdev6894
      @bsdev6894 4 роки тому

      @@BMoDev I fixed it thanks for the reply.

    • @IbbosChannel
      @IbbosChannel 4 роки тому

      @@bsdev6894 how did you fix that? I think im having the same issue

    • @frankaors5223
      @frankaors5223 3 роки тому

      @@bsdev6894 how did you fix that?

  • @thecanaeggplant2937
    @thecanaeggplant2937 4 роки тому

    Please help me i am literally having a pain

  • @lordwigstyle8017
    @lordwigstyle8017 Рік тому

    I want it to have 3 colors tho, but it will only go green and yellow, and it will never reach the 3rd else if, pls help
    void Update()
    {
    if (slider.value slider.minValue && !fillImage.enabled )
    {
    fillImage.enabled = true;
    }
    float fillValue = liv.CurrentHealth / liv.MaxHealth;
    if (fillValue >= slider.maxValue / 1.7)
    {
    fillImage.color = Color.green;
    }
    else if (fillValue < slider.maxValue / 1.7)
    {
    fillImage.color = Color.yellow;
    }
    else if(fillValue

  • @SneakyTurtle
    @SneakyTurtle 4 роки тому +1

    gonna waste my time and now show how you made the player health script ? nike

    • @BMoDev
      @BMoDev  4 роки тому +2

      PlayerHealth script just holds 2 floats, no functionality:
      public class PlayerHealth : MonoBehavior
      {
      public float currentHealth = 10;
      public float maxHealth = 10;
      }
      let me know if you need help :)

    • @krjamesjr
      @krjamesjr 4 роки тому

      @@BMoDev I was looking for this as well; thanks!

  • @whitekanyewestthebully1346
    @whitekanyewestthebully1346 3 роки тому

    5:55

  • @xuyixz8229
    @xuyixz8229 4 роки тому

    how can we make the health bar withou the health script thanks for wasting my time

    • @BMoDev
      @BMoDev  4 роки тому +1

      The script has 2 float variables
      public class PlayerHealth : Monobehavior
      {
      public float currentHealth;
      public float maxHealth;
      }
      Are you able to get it working?

    • @xuyixz8229
      @xuyixz8229 4 роки тому

      BMo already fixed it but thanks

  • @Lavender34124
    @Lavender34124 2 роки тому +1

    i can't add the script into the Slider cause it says the script class cannot be found

    • @Lavender34124
      @Lavender34124 2 роки тому

      got it working

    • @natural2018
      @natural2018 2 роки тому

      @@Lavender34124 how did you get it to work I'm getting the same errors, I fixed the script and image but the player health is still not working

  • @mohammedjawad3075
    @mohammedjawad3075 4 роки тому

    whats the player script?

    • @BMoDev
      @BMoDev  4 роки тому +2

      public class PlayerHealth : MonoBehaviour
      {
      public float currentHealth = 10;
      public float maxHealth = 10;
      }