You should really use the actual names of Unity’s magic methods rather than ‘Begin’ ‘Loop’ ‘End’. It makes your examples hard to understand, and there is important distinctions between Awake, OnEnable and Start that are best not to be glossed over.
But the order of events between multiple objects isn’t something you should rely on. Nor is there any difference between Runtime and Editor. There is also no ‘Reset’. The opening scene you play is a copy of the scene on file.
Simply having any dependencies happen on separate callbacks for initialisation (Awake, OnEnable, Start) is safe enough to avoid timing issues.
Yes, because the scene you’re playing is merely a copy of the one you were editing. The is no actual ‘event’ that resets the data. The copy during play mode is simply discarded and the instance you were editing returned.
It is purposefully undefined, thus, undocumented. Hence why you shouldn’t rely on it. It’s not been neglected, it is purposefully omitted to not create any expectations.
I think you’re wasting your time. This is stuff you don’t have to think about in 99.99% of cases, and in the 0.01% you’re likely doing something wrong.
Execution order between game objects is undefined and while it stays consistent most of the time, it can change at any point. And it will almost certainly change for different platforms - typically that’s when users report “works in the editor, fails in build” they were likely unaware of that and the side effects.
For each GameObject that does process, it will run each component’s event methods in the order they are listed in the Inspector:
A: Awake
A: OnEnable
B: Awake
B: OnEnable
A: Start
B: Start
A: Update
B: Update
A: OnDestroy
B: OnDestroy
Assuming this code:
var instance = Object.Instantiate(existing);
instance.AddComponent<B>();
And assuming existing has a component “A” on it:
A: Awake
A: OnEnable
B: Awake
B: OnEnable
Note that Awake/OnEnable for A run before Instantiate returns, and for B these run before AddComponent returns.
Irrelevant. Runtime + Editor is the same. Disabled objects are, well, disabled - that means no Update/FixedUpdate/LateUpdate and most other events. Destroying a disabled object will run OnDestroy of course.
PS: You get fine answers to such questions from any code-aware AI.
And then there is the Script Execution Order settings which may mess things up further ^^.
The point here is, there is no one dimentional flow of the whole engine that can be drawn out in a fix manner. As it was already mentioned, without specific Script Execution Order for individual components, you can not rely on a particular order. If the order is important, you should take measures yourself. Either use the Script Execution Order to put certain scripts before / after normal time or use your own methods which you would call yourself at the time necessary.
Generally speaking the processing of gameobjects follows the order in which they are created in memory. Though this order can not really be influenced and may differ between editor and build. It may even change between two builds, that’s why you should not rely on it.
The script execution diagram tries to map out the events for a single component and even here many external factors aren’t included. Like an already active gameobject and script does not get another Start callback when you disable / enable it at a later time, but OnEnable will be called again. This diagram is an aggregated summary of most callbacks and how they roughly relate to the lifetime of the script.
If you wanted to map out the actual control flow of EVERYTHING you would need actual conditional branches in the flow and the diagram would get extremely wide and confusing.
There are also differences when a prefab is intantiated whether it was enabled / active before or not. You can not document the whole behaviour in a single diagram.
In general, you can’t rely on the order in which the same event function is invoked for different GameObjects, except when the order is explicitly documented or settable.
You can’t specify the order in which an event function is called for different instances of the same MonoBehaviour script. For example, the Update function of one MonoBehaviour might be called before or after the Update function for the same MonoBehaviour on another GameObject, including its own parent or child GameObjects.
To configure the execution order between different MonoBehaviour scripts, refer to Script execution order.
The following diagram provides a high-level overview of the execution sequence for event functions that run during the lifecycle of a MonoBehaviour script component. For readability, the scope of the chart is limited to key parts of the script lifecycle, with some extra internal subsystem updates provided for context.
OnDisable appears to lead directly to OnDestroy. An arrow leads from OnDestroy to OnEnable.
That, except for the arrow, might be / is the flow after application quitting, it is not for general disabling / enabling of scripts during lifetime.
Does SetActive(false) trigger OnDisable() ? Apparently GameObjects can be active or inactive. I don’t see this distinction and the effect of either setting represented in this documentation.
The beginning part of the documenting diagram has Start following OnEnable .
This look and the first line in the documentation of StartUnity - Scripting API: MonoBehaviour.Start() “Start is called on the frame when a script is enabled just before any of the Update methods are called the first time.” sound like Start always follows OnEnable, so in Disable/Enable flows too.
Only the text besides the Start event in the diagram and the second line in the documentation of Start let give the possibility to become aware of the role of Start.
I welcome the provision of documentation of the full player loop. After all, what is a documentation presentation about? And this for a key part of the Unity game engine exposed to its users.
I find it fascinating how millions of Unity users can structure their code successfully to interoperate with the Unity engine as it is and as it is documented, and yet we still periodically have threads like this one grinding on and on ceaselessly.
May I suggest you adjust your strategy to one that accommodates robust code that does not rely on one or the other timing edge case? I promise you will find more happiness that way, as well as make a lot more forward progress with your games.
Or else go ahead and just thumbs-down this reply like you have the others above and then we can all get back to making our games, knowing you’re not interested in doing that yourself.
I know @MelvMay has had complaints about the execution order chart before, since (iirc) it’s slightly wrong about when OnTriggerXXX/OnCollisionXXX happens.
Ordering is pretty complex, which is why Unity developers generally try to avoid writing code that relies on it. In systems where ordering is non-complex, you can write a lot simpler code by having a sensible order and then allowing things to make assumptions about it.
You honestly don’t need to care about the full playerloop 99.99% of the time. There’s no need for it to be documented and it would be a waste of Unity’s time.