Variable doubling when function is run more than one time

public GameObject damage;
public float damageTaken;
public GameObject EnemyHP;
public int EnemyHealth;
public TMP_Text EHP;
public GameObject EnemyHealthHP;
public float MaxHP;
public UnityEngine.UI.Button hit;
public GameObject load;
public float currentHP;
public float resize;


private void Start()
{
    
}
// Update is called once per frame
void Update()
{
    if (load.GetComponent<Attack>().enemyload == true)
    {
        Debug.Log("ENEMYLOAD");
        MaxHP = load.GetComponent<Attack>().EnemyMaxHealth;
        EnemyHealth = 200;
        EHP.text = MaxHP + "/" + MaxHP;
        load.GetComponent<Attack>().enemyload = false;
        currentHP = MaxHP;
    }
    hit.onClick.AddListener(Attack);
}

void Attack()
{
    if (load.GetComponent<Attack>().dmgload == true)
    {
        Debug.Log("EnemyDMG");
        damageTaken = damage.GetComponent<Attack>().damage;
        Debug.Log(damageTaken);
        currentHP -= damageTaken;
        resize = currentHP / MaxHP;
        RectTransform EnemyHP = GetComponent<RectTransform>();
        EnemyHP.sizeDelta = new Vector2(EnemyHealth*resize, EnemyHP.sizeDelta.y);
        EHP.text = currentHP + "/" + MaxHP;
        load.GetComponent<Attack>().dmgload = false;
    }

This is the code for controlling the enemy health in my game. It works perfectly the first time the attack button is pressed, but the second time the amount of damage that is supposed to be dealt doubles. Lets say damageTaken is 10 and the maxHP is 20. After the first attack currentHP is 10, however after the second attack currentHP becomes -10, on the third it becomes -50, and so on. damageTaken doesn’t change during this and does stay 10 every time, however current HP takes double what it did the previous time for reasons unknown to me. Everything else is currently working except currentHP of the enemy and player (both player and enemy scripts are nearly identical other than the variables they are taking from the script and the script controlling player has a coroutine to wait till the enemy deals damage). Any help is greatly appreciated!!

Staring at dense sheets of code is rarely useful. Code isn’t super-relevant. What data the code is operating on at runtime is far more interesting. Find where that doubling happens. Is the variable doubled? Is it executing twice? Do you have more than one copy of the script working? etc.

Edit: you’re adding a listener in Update()… that’s gonna keep adding your listener again and again. That is never going to be a GoodThing™ for your program’s lifecycle because every one of those WILL get called, even if your boolean inhibits some functions.

Honestly it just sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

How would I go about removing the listeners?

By putting that line in Update() it runs every frame, adding that same listener again and again.

Generally you have one listener, added once. Add it in Start().

After that you usually don’t have to remove it.

If you want to add / remove it, do so in OnEnable / OnDisable

Here’s just one random example: scroll down:

1 Like