Need help with my Enemy Health bar.

First can you look at the video I did record.

If I just attacking Enemy 1, it’s working, but when I go and attack Enemy 2 the Health bar getting value from Enemy 1.

I have made the Enemy Health bar with UI Slider

Player Script:

public class Player : MonoBehaviour
{
    public string name = "Player";
    public int swordDamage = 15;
    public float range;
    public static Transform opponent;
    public float health = 100;

    // Update is called once per frame
    void Update ()
    {
        Attack ();
    }


    void Attack()
    {

        if (Input.GetKeyDown (KeyCode.Mouse0))
        {
            if (opponent != null && Vector3.Distance (opponent.position, transform.position) < range)
            {
                opponent.GetComponent<Enemy> ().GetHit (swordDamage);
            }

        }
    }
     
}

Enemy Script:
```
**public class Enemy : MonoBehaviour
{
public string name = “First Enemy”;
public float damage;

// Enemy Health
public float health = 100;
public Slider healthBar;
public Text healthText;


public void GetHit(float playerDamage)
{
    health = healthBar.value -= playerDamage;

}



void OnMouseOver()
{
    // Target each enemy with hover.
    Player.opponent = transform;
    Debug.Log (gameObject.name);

    // Showing Health Bar Text on Hover.
    healthText.text = health.ToString() + " / 100".ToString();
}

void OnMouseEnter()
{
    // Showing Health Bar
    GameObject.Find ("EnemyHealthBack").GetComponent<Image> ().enabled = true;
    GameObject.Find ("EnemyFullHealth").GetComponent<Image> ().enabled = true;

    // Showing the Health Text.
    healthText.GetComponent<Text> ().enabled = true;
    healthText.text = health.ToString() + " / 100".ToString();
}

void OnMouseExit()
{
    // Hidding the Health Bar
    GameObject.Find ("EnemyHealthBack").GetComponent<Image> ().enabled = false;
    GameObject.Find ("EnemyFullHealth").GetComponent<Image> ().enabled = false;

    // Hidding the Health Text.
    healthText.GetComponent<Text> ().enabled = false;
}

}**
```
EDIT: Sorry I didn’t use CODE tags.

Please use code tags! Edit your post, and paste the code again in code tags, otherwise it’s near impossible to read the code.

Now did i fix it thanks.

Okay, looking at the video, it seems like you have the same healthbar slider assigned to each enemy:

public Slider healthBar; //This one

Which means that when an enemy gets hit:

public void GetHit(float playerDamage)
{
    health = healthBar.value -= playerDamage;
}

You’re setting the enemy’s health to the global health bar’s value, minus the damage.

You should reset the health bar to the current health when you mouse over the enemy:

    void OnMouseEnter()
    {
        // Showing Health Bar
        GameObject.Find ("EnemyHealthBack").GetComponent<Image> ().enabled = true;
        GameObject.Find ("EnemyFullHealth").GetComponent<Image> ().enabled = true;

        // Showing the Health Text.
        healthText.GetComponent<Text> ().enabled = true;
        healthText.text = health.ToString() + " / 100".ToString();
        healthBar.value = health;
    }

The change in bold.

I also think you should avoid setting the health to the value of the health bar - even if (I believe) the code will work with only the line above, I think your GetHit should look like this:

public void GetHit(float playerDamage)
{
    health -= playerDamage;
    healthBar.value = health;
}

That’ll make the health bar take the value of the health, rather than the health take the value of the health bar, which is a lot cleaner.

That works great, I’m very new with C# and Unity. I trying to learn how to use everything hehe. But very thanks for that answer! Working great now.