Instantiate on value change

I’ve search an answer for this simple question but didn’t found something really helpful.
In my project I have a script with a simple int variable “Damage” that store the amount of Damage the character receive everytime he gets hit by a trap.
I just want to instantiate a damage text each time his “Health” value change. I thought this would be really easy, something like Damage.OnValueChange… But I can’t figure out how to do that !

You can use System.Action from msdn C# like this:

public class MyClass : MonoBehaviour
    public Action onHealthChange;
    public int myHealth = 5;

    public void AddHP ( int amount)
          myHealth += amount;
          if ( amount != 0 && onHealthChange != nulll )

Then in another class you could do:

public class AnotherClass : MonoBehaviour
     public MyClass mc;
     void OnEnable()
            mc.onHealthChange += MyFunction;
     void OnDisable()
            mc.onHealthChange -= MyFunction;

    void MyFunction()
          Debug.Log("Health changed");

You can also make onHealthChange static, and then just suscribe to that event like MyClass.onHealthChange += MyFunction;

His health value changes when he takes damage, call your function from there.

Thanks for the answers I’m trying to make this work

It would depend on where you apply the damage to the health.
just write the line:

Instantiate([yourGameObject], transform2Inst, Quaternion.identity) as GameObject;

somewhere inside the same method.

call the funciton to instantiate at the same place where you modify the health from the damage taken…