简体   繁体   中英

Updating Enemy Health bar in unity 2d

Why is this connecting all the health bars of my enemies together, even though their actual health is decreasing at its specified rate?

public class FillHealth : MonoBehaviour
{
    Image HealthBar;

    private NormalMonster normalMonster;
    // Start is called before the first frame update
    void Start()
    {
        HealthBar = GetComponent<Image>();
        normalMonster = GameObject.FindGameObjectWithTag("Normal Monster").GetComponent<NormalMonster>();
    }

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

    public void UpdateHealthLeft()
    {
        if (normalMonster.healthLeft > 0)
        {
            HealthBar.fillAmount = normalMonster.healthLeft / normalMonster.setHealth;
        }
    }
}

This is the script that is being referenced in FillHealth. As far as I understand it, since the variable isn't static, then the values should not be shared. It should find fill the health bar for each individual enemy.

public class NormalMonster : MonoBehaviour
{
    private float _normalSpeed = 2f;

    private float _BaseHealth = 20f;
    private float _HealthModifier;
    public float setHealth;
    public float healthLeft;

    

    // Start is called before the first frame update
    void Start()
    {
        UpdateHealth();
    }

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

    public void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Arrows")
        {
            healthLeft -= Arrows.Damage;
            Destroy(other.gameObject);

            if (healthLeft <= 0f)
            {
                Destroy(this.gameObject);
                EarnedGold.earnedGold += 7;
                Spawn_Manager.enemyCount--;
            }
        }
    }

    public void UpdateHealth()
    {
        if (StageMode.StageLvl > 5)
        {
            _HealthModifier = (StageMode.StageLvl * 0.01f) * _BaseHealth;
            setHealth = Mathf.Round(_BaseHealth + _HealthModifier);
        }
        else
        {
            setHealth = _BaseHealth;
        }
        healthLeft = setHealth;
    }



    public void NormMonMov()
    {
        transform.Translate(Vector3.left * _normalSpeed * Time.deltaTime);
        transform.position = new Vector3(Mathf.Clamp(transform.position.x, -7.0f, 10), transform.position.y, 0);
    }


}

Any help would be greatly appreciated for this guy who just start playing with unity this weekend.

Instead of getting the image, get the rect transform, like this

public RectTransform healthBar;

and change the length with:

healthBar.sizeDelta = new Vector2(normalMonster.healthLeft,healthBar.sizeDelta.y);

I believe the issue is with normalMonster = GameObject.FindGameObjectWithTag("Normal Monster").GetComponent<NormalMonster>();

If you have two Monsters Both have two scripts attached, FillHealth and NormalMonster Both the "FillHealth" scripts look for the FIRST gameobject in the scene that has a script with tag NormalMonster so both monsters are pointing to the exact same NormalMonster script (the first in the list)

Change "GameObject" capital G to "gameObject" lower case g

Still not the best way to code this, but that may work I think

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM