[C#]Enemy health not decrementing

I have a class called Enemy which basically has a field called enemyHealth which is equal to 50 and a method called reduceHealth which takes in a float parameter called damage. Inside the method I have this statement enemyHealth -= damage;. I also have a debug.log to tell me if the method is getting called when the bullet hits the enemy player and it is because I get a log telling me so. However, the enemy health doesn’t seem to be decremented. Any help?

If you could paste your code that would help us help you.

Can you add another debug.log statement to show the health before and after the enemyHealth -= damage? Or even set a debug point and see what is going on? How are you determining that the health is not being decremented?

It was silly of me not to check the health itself. I used the unity editor to check but apparently that doesn’t update. The health is decremented actually. Now, I just need to delete the object. Cheers :slight_smile:

It sounds strange that the Unity Editor is not updating the value as you play. It sounds like you may have a variable name mismatch somewhere perhaps or even a scoping issue? That is where seeing the code would help us to give you a more accurate source of the problem. I’m glad you have verified that it is indeed working though! Good luck!

  public float enemyHealth = 500;

  public void reduceHealth(float damage)
    {
        Debug.Log(enemyHealth);
        enemyHealth -= damage;
        Debug.Log(enemyHealth);

        if (enemyHealth <= 0)
        {
            enemyHealth = 0;
            this.gameObject.SetActive(false);
        }
    }

Here’s the method right here.

Very strange, looks completely legitimate to me! Maybe something strange going on w/ the Unity Inspector…

Yeah, I know. I think I’m going to delete the Enemy prefab and re-create it and see if that solves the problem.

Yeah, I feel like something is amiss here. Perhaps restart the Unity Editor while you are at it.

Where are you calling the function?