Variable only updates when visible.

Hi there,

I have a very weird bug, currently i have a top down shooter set up and today was my last day of development, until this bug happened.

Basically i have 4 towers the player has to defend, when enemies are attacking these towers and they are in view of the player the towers health is updated fine, but when it gets out of view it’s variable is no longer updated.

Does anyone have any ideas?

Cheers

Building Health Code

public class BuildingHealth : MonoBehaviour {

    public EnemyAI ai;
    public int structureID;
    public float health;
    public Transform target;
    public GameObject explosion, debris, hitEffect;
    public void Start()
    {
        target = GetComponent<Transform>();
        structureID = GM.Instance.defences.Count;
        GM.Instance.defences.Add(this.gameObject);
        tag = "Player";
    }

    public void LoseHealth(float amount)
    {
        if (health <= 0)
        {
            Dead();
        }
        health -= amount;
    }

    public void Dead()
    {
        GameObject newExplosion = (GameObject)Instantiate(explosion, transform.position, Quaternion.identity);
        GameObject newDebris = (GameObject)Instantiate(debris, transform.position, Quaternion.identity);
        tag = "Untagged";
        GM.Instance.DestroyDefence(this.gameObject);
        Destroy(this.gameObject);
    }

    void OnTriggerEnter(Collider hit)
    {
        if (hit.collider.gameObject.tag == "Enemy")
        {
            GameObject newHit = (GameObject)Instantiate(hitEffect, transform.position + new Vector3(0,2,0), Quaternion.identity);
            ai = hit.collider.transform.root.GetComponent<EnemyAI>();
            LoseHealth(ai.damage);
        }
    }
}

The enemy attack is just animations as i am detecting health loss through triggers.

You mentioned the enemy attack are animations, right? So check if the Animation component has the CullingType enum as “Always Animate” and are not “Based On Renderers”. I think that’s pretty much it.