Run a function once everytime value changes

Hello developers…
I’ve searched all over the internet but I can’t seem to find the answer to my question (Maybe I am not searching in the right places)…

The variable “value” is dynamic and can change anytime
On the bottom I have a RunOncePerSwitch() function
What I really want to is when a state switches(for example healthy to several or several to critical) this function to run only once!
How can I achieve that?

Since you already have a method to change the state, can just use that to trigger it.

if(value < 100 && value > 75)
{
    if(currentState != state.Healthy;
    {
        currentState = state.Healthy;
        RunOncePerSwitch();
    }
}else if...

Every time you attempt to change the value check if the value is actually different and if it is, then run your function. This can be done in a clear manner using properties (or by implementing similar functions). i.e.

private int _hp = 100;
private state _currentState = state.Healthy;

public int hp
{
    get  {  return _hp;  }
    set
    {
        _hp = value;
        if (_hp < 100 && _hp > 75)
        {
            currentState = state.Healthy;
        }
        else if (_hp < 75 && _hp > 50)
        {
            currentState = state.Several;
        }
        else if (_hp < 50 && _hp > 25)
        {
            currentState = state.Critical;
        }
        else //if (_hp < 25)
        {
            currentState = state.Dangerous;
        }
    }
}
public state currentState
{
    get  {  return _currentState;   }
    set
    {
        if(_currentState != value)
        {
            _currentState = value;
            RunOncePerSwitch();
        }
    }
}

public enum state
{
    Healthy,
    Several,
    Critical, 
    Dangerous,
}

void RunOncePerSwitch(){
   //I want this to run once everytime a state switches
}

I also changed value to hp since value is a reserved keyword used in properties and made hp into a property so hp changes are “instant” and don’t need to wait a frame (hence why there is no update, TestinFunction, or changeState functions).