Are Event functions executed for ALL monobehaviors before stepping to the next Event function?

Disclaimer: sorry if this question is silly, I have always been assuming the answer to be ‘yes’ but cannot find it explicitly in the doc :sweat_smile:

In the Event function execution order doc, we see that, for each monobehavior, events functions are executed in a specific order.

Question: when the loop executes 1 event function (for instance Update), is it executing it for ALL the monobehaviors in the scene before steping to the next event function to be executed (for instance yield null)?

Example: we see on the diagram that OnTriggerXXX comes before OnCollisionXXX. Can I confidently say that the loop is going to execute OnTriggerXXX for ALL monobehaviors implementing it and ONLY THEN execute OnCollisionXXX for ALL monobehaviors implementing it? Is it true for all event functions?

Note: in the doc, it is specified that we cannot rely on the execution order of 1 single event function between GameObjects. My question phrased differently is : can we rely on the execution order between event functions?

Answer is no, but it depends on the event.

For example, Awake and OnEnable get called together for each component.

It might be the case for others. You should do simple tests to check this.

Question is, for what reason is this a concern?

Oh Ok! Thanks!

So this might be true for some events. But IMO I cannot rely on tests (they could work on my machine and not others).

I have:

  • Script A and Script B set a boolean during OnCollisionEnter
  • Script C reads this boolean after yield WaitForFixedUpdate

I need to be sure script A and script B updated the boolean correctly before script C reads it. This is the case if OnCollisionEnter is executed consistently through ALL monobehaviors BEFORE yield WaitForFixedUpdate. In my tests it works, but if it is not by design, then I cannot rely on it.

I’d say the timing of event functions isn’t going to change per system. That would make the engine incredibly unreliable.

If it works in your tests you should be good. Any change to that in the future would be a big breaking change so I doubt Unity would change this.

Those are dedicated callbacks for the physics system which calls them directly at the end of a simulation step. The physics system doesn’t spend its time compiling a list of MonoBehaviours, sorting them by “execution order” then calling them in that order. That goes for all sorts of system-specific callbacks from (say) the particle or animation systems.

The execution order only applies to the global generic MonoBehaviour callbacks such as “Awake” or “Update”.

It does not apply to systems actively calling their own dedicated callbacks as they’re unrelated to “script execution order”, they are executing and they’re performing callbacks.