K
K
kitmen2019-02-11 16:41:34
C++ / C#
kitmen, 2019-02-11 16:41:34

Unity(C#) — How to fix change of value by variable in Update?

When a variable (int) changes its value, I need to call a method that will work 1 time in Update.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2019-02-11
@kitmen

private int oldValue;

void Start() 
{
    oldValue = 42
}

void Update()
{
    if (oldValue != newValue)
    {
        OnValueChanged(newValue);
        oldValue = newValue;
    }
}

private void OnValueChanged(int newValue) 
{
}

But in general it is better to look towards events and delegates, or even (especially if there are many such places) Unity Atoms .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question