How to make multiple Enemy Health Bars to work?

Hello everyone! I’m in dire need of some help. I currently working on a beat 'em up and I manage to get the enemy health bar to work, however, whenever I place more than one enemy within the scene only one is displayed over being shown while the others are blank. Here’s the tutorial I’ve been following => ( SGD 213 Programming II - YouTube ) and I’ll also post my code for everyone to see…

public class Stats: MonoBehaviour
{
    // ~~~~~~~~~~~~~~~~Variables~~~~~~~~~~~~~~~~
    #region
    // ~~~~~~~~~~~~~~~~Public~~~~~~~~~~~~~~~~
    public float health;
    public float strtHsd; //Starting Health
    public bool dispUI; //Display UI
    public Slider hsdSlider; // Health slider
    public GameObject hsdUI; // HealthUI
    public bool enemy2Win;
    // ~~~~~~~~~~~~~~~~Private~~~~~~~~~~~~~~~~
    //private object GameManager; // Just add a private variable.

    // ~~~~~~~~~~~~~~~~Others~~~~~~~~~~~~~~~~
        #endregion

    void Awake()
    {
        health = strtHsd;
    }

    void Update()
    {
        if (gameObject.tag == ("Player"))
        {
            hsdUI = GameObject.FindGameObjectWithTag("PlayerHealthUI");
            hsdSlider = hsdUI.gameObject.transform.GetChild(0).GetComponent<Slider>();
            if(hsdSlider.maxValue == 0)
                hsdSlider.maxValue = strtHsd;
                hsdSlider.value = health;
        }
        if (gameObject.tag == ("Enemy") /*&& dispUI == true*/)
        {
            hsdUI = GameObject.FindGameObjectWithTag("EnemyHealthUI");
            hsdSlider = hsdUI.gameObject.transform.GetChild(0).GetComponent<Slider>();
            if (hsdSlider.maxValue == 0)
                hsdSlider.maxValue = strtHsd;
            hsdSlider.value = health;
        }
        else if (gameObject.tag == ("Enemy"))
        {
            hsdUI = null;
            hsdSlider = null;
        }
        if (health <= 0)
        {
            //Destroy(gameObject.transform.parent.gameObject); If there is a gameobject that does not have a parent attached, the code has nothing to destroy.
            Destroy(gameObject.transform.gameObject);//Instead of destroying just the sprite, we'll be destroying the enemy spawner location.
        }

        if (enemy2Win == true)
        {
            GameManager.instance.GameOver();
        }
    }

}

Good day.

I did not read your code carefully to understad what are you doing. But for your question i can undesrstand you have some basi structure problems…

First, all enemies must have its own script, and irt ts own healthbar, so each script “talks” with its healthbar.

Soooo, i see this code:

 if (gameObject.tag == ("Enemy") /*&& dispUI == true*/)
         {
             hsdUI = GameObject.FindGameObjectWithTag("EnemyHealthUI");

Here, you are saying this: If “I’m an enemy”, the hsdUI is the first object found in the scene with tag “EnemyHealthUI”

As you cna imagine this is 100% incorrect, there are so many healthbars in the scene, so it will not work, i need that each enemy script talks only with its own bar, you should use something like:

             hsdUI = gameObject.transform.Find("HealthBar");ç

To find the healthbar child of the object…

I strongly recommend to learn how to acces all components, before commence doing a complex tutorial, or you will get lost like now. Go watch some basic tutorials about referencing, accesing, and seting variables.

Bye!

Without a screenshot, I"m not sure what you’re seeing, but based on the code I would guess that your hsdsSlider(s) may overlapping. To see if that’s the case you could try setting the hsdSlider transform position in Awake to something unique, maybe based on an “id” inspector var. Ex:

public int id = 1;  //set this to a unique value for each character in the inspector/editor

then in Awake set the hsdSlider position based on the id. Ex:

void Awake() {
   hsdSlider = hsdUI.gameObject.transform.GetChild(0).GetComponent();
   hsdSlider.transform.position = hsdSlider.transform.position * id;//not final solution, just to prevent overlapping
}

also, just a suggestion for improving performance, don’t use “FindGameObjectWithTag” in Update. Get a reference to the object once in Awake (or maybe better to do in Start), and then use that reference in Update.