healthbar does not slide when damage is taken

I followed brackeys tutorial on how to do health bars but when I take damage the bar doesn’t go down it just disappears when it reaches zero how do I fix this here are the two codes I used. Help would be greatly appreciated as I am stupid.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class healthbar : MonoBehaviour
{

    public Slider slider;

    public void setmaxhealth(int health)
    {
        slider.maxValue = health;
        slider.value = health;
    }

    public void sethealth(int health)
    {
        slider.value = health;
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Player : MonoBehaviour
{

    public int maxhealth = 100;
    public int currenthealth;
    public healthbar healthbar;

    // Start is called before the first frame update
    void Start()
    {
        currenthealth = maxhealth;
        healthbar.setmaxhealth(maxhealth);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            takedamage(20);
        }

        void takedamage(int damage)
        {
            currenthealth -= damage;

            healthbar.setmaxhealth(currenthealth);
        }
    }
}

First, put your takedamage() method OUTSIDE of the Update() method, but obviously still in the class. There’s no need for that to be a local method. That might even be causing a compiler error.

Beyond that, you must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Please post on the scripting forums for posts that are simply about scripting and nothing about 2D specifically.

@ducklanks Wouldn’t the following code set the slider to 100% each time? The current value is set equal to the max value.

 public void setmaxhealth(int health)
    {
        slider.maxValue = health;
        slider.value = health;
    }

You my friend are a absolute star

1 Like