Variable updates everywhere but Update function.

I have a variable “health” this is effected when damaged. When i debug.log the variable in the update function, it does not change, it still says the health = 100, but when i debug.log health in any other function, the variable is updated, but still stays the same in update.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class enemyController : MonoBehaviour {


    private Vector3 origin;
    private GameObject player;
    

    private NavMeshAgent nMA;

    public enemyType enemyType;
    
    private bool isDead = false;
    private bool attackingState;
    private bool isAttacking;
    private float maxHealth;
    private float health;
    private int level;
    private float attackDMG;
    private Animator enemyAnim;
    private bool patrollingState;
    private bool isPatrolling;

    // Use this for initialization
    void Awake()
    {
       

    }

    void Start () {
        this.origin = this.gameObject.transform.position;
        nMA = this.gameObject.GetComponent<NavMeshAgent>();
        isAttacking = false;
        isPatrolling = false;
        attackDMG = enemyType.baseDamage;
        player = GameObject.FindGameObjectWithTag("Player");
        enemyAnim = this.gameObject.GetComponent<Animator>();
        level = enemyType.baseLevel;
        maxHealth = enemyType.maxHealth;
        health = 100;
    }

    public void switckIsAttacking()
    {
        if (isAttacking)
        {
            isAttacking = false;

        } else 
        {
            isAttacking = true;
        }
    }

    public float getHealth()
    {
        return health;
    }

    public float getMaxHealth ()
    {
        return maxHealth;
    }

    public float damageHealth(float dmg)
    {
        Debug.Log("damageHealth: " + health);
        if (!isDead) {
            health = health - dmg;
        }
        
        return health;
    }
	
    public void Die()
    {
        nMA.isStopped = true;
        enemyAnim.SetBool("Dead", true);
    }

    public float getDamage()
    {
        return attackDMG;
    }

    public void Attacking()
    {
        
       if (!isDead)
        {
            
            if (this.gameObject.tag == "cQBEnemy" && Vector3.Distance(this.gameObject.transform.position, player.gameObject.transform.position) <= 3)
            { 
                nMA.isStopped = true;
                this.transform.LookAt(player.transform);
                enemyAnim.SetBool("Attacking", true);
            } else
            {
                nMA.isStopped = false;
                nMA.SetDestination(player.transform.position);
                enemyAnim.SetBool("Attacking", false);
               

            }

        }     
        
    }

    public void patrolling()
    {
        StartCoroutine("patrollingWaitTime");
    }

    IEnumerator patrollingWaitTime()
    {
        isPatrolling = true;
        nMA.SetDestination(new Vector3(Random.Range(origin.x-3, origin.x+3), 0, Random.Range(origin.z-3, origin.z+3)));
        yield return new WaitForSeconds(Random.Range(5, 10));
        patrolling();  
    }

    public void Kill()
    {
        Destroy(gameObject);
    }

    public void checkStates()
    {
        if ( Vector3.Distance(this.gameObject.transform.position, player.gameObject.transform.position) <= 20)
        {
            isPatrolling = false;
            patrollingState = false;
            attackingState = true;
        } else
        {
            if (Vector3.Distance(this.gameObject.transform.position, player.gameObject.transform.position) >= 20)
            {
                attackingState = false;
                if (Vector3.Distance(this.gameObject.transform.position, this.origin) >= .5f)
                {
                    if (!isPatrolling)
                    {
                        nMA.SetDestination(origin);
                        patrollingState = false;
                    }
                    
                } else
                {
                    if (Vector3.Distance(this.gameObject.transform.position, this.origin) <= .5f)
                    {
                        if (!patrollingState)
                        {
                            
                            patrollingState = true;
                            patrolling();
                        }
                    }
                }
                

                
            }
            
            
        }
    }

    public void checkHealth()
    {
        if (health <= 0)
        {
            Debug.Log(this.gameObject.name.ToString() + " " + health);
            health = 0;
            isDead = true;
            Die();
        }
    }
    
    
    // Update is called once per frame
    void Update () {
        Debug.Log("Update: " + health);
        checkStates();
        Vector3 vel = nMA.velocity;
        float speed = vel.magnitude; 
        enemyAnim.SetFloat("speedPercent", speed * 10);

       

        if (attackingState)
        {
            isPatrolling = false;
            patrollingState = false;
            Attacking();
        }

        checkHealth();

        

        
        
    }
}

How does your script even supposedly compile with this line??

public enemyType enemyType;

Class names start with an uppercase letter because of things like this.

That aside, once I fixed this error, I ran the script and it worked just fine.

Console output

My guesses for your problem are:

  1. You have multiple enemies in your scene. You damage one, but then monitor the health of the other(s).
  2. You have another component (probably a script) that interferes with your health variable every update
  3. You are reading your Console wrong. Dunno how though :slight_smile: