Race conditions in Unity - Is this a problem?

Hi there,

I think I know the answer to this, but I’ve not found it definitively via my searches (always hard to pinpoint the exact wording isn’t it?).

I’m using a colliders OnTriggerEnter() and OnTriggerExit() to keep a list of potential targets in range of my turret. The search for a new target is done via the Update() script. If it’s possible that OnTriggerExit() could be called while Update() was still doing its checking, I could end up in a nasty little situation of suddenly attacking an out-of-range enemy.

I can (of course) factor this into my code, but it would all get very messy with additional double-checks for the various scenarios.

So… Could OnTriggerExit() and Update() be called concurrently?

No ! It can’t be called, you can look there to see the order of execution :

Even if there are multiple events in the same frame, update is called after the physics events.