Multiple animation events call the same function on a singleton instance, what happens?

Hi, I have a question about concurrency, i cannot find answer anyware.
Example: I have two instance of prefab, prefabA and prefabB, each of them has an animation with a event, and than i have a singleton class with a function F.
I think it’s almost impossbile, but what happens if prefabA and prefabB events are called at same time and each of them call function F? In the following example can i be sure that F is called two times, sequentially? Or can it happens a concurrency situation, for example while F is executed by prefabA, prefabB call F and value become 2 before the if and //do stuff not executed?

Singleton class:

int value = 0;

public void function F()
{
     value++;
     if (value ==1)
     {
        // do stuff
        value = 0;
     }
}

Thanks

Unity runs on single thread, it is not possible that some code will step in the middle of execution.
Everything is sequential, unless you intentionally use the job system or something like that, but that cannot happen by mistake :smile:

1 Like

Because Unity runs on a single thread, in your thought example, events are resolved in some undefined order, sequentially. Most likely in reverse order from how the listeners were subscribed to the events, but system offers no guarantees for this.

1 Like

Perfect! Thanks to everyone