Add Listener

By using AddListener, you can add your custom callback to the onChange Event.

public class GKObserverHealthOwner : MonoBehaviour
{
    public FloatVar health;
    public FloatVar maxHealth;
    public BoolVar isDied; // A bool var

    void Awake()
    {
        health.Value = maxHealth.Value;
        health.AddListener(OnHealthChanged); // Add Listener
    }
    
    //The Listener
    void OnHealthChanged(float oldHealth, float newHealth)
    {
        if(newHealth <= 0){
            isDied.Value = true; // Set isDied to true if new health <= 0
        }
    }
}

Last updated