6
votes

The Unity Manual describes the order in which the Script functions are called. However, I was wondering if there were any rules regarding the order in which the GameObjects themselves are considered in Unity.

GameObjects are basically the nodes of Unity's scene graph and (assuming the scene itself was the root node) they form a tree. I was wondering if that tree structure imposed any rules on the order in which GameObjects are considered.

As already mentioned, the manual describes that Awake() is always called before Start() which is always called before the first call to Update() and so on. However, these relations in time are (mostly) given in scope of a single script on a single GameObject. I want to know if there is also a rule stating the order in which Start() (or any other method) is called on all the GameObjects in the scene.

Specifically I wanted to know:

  1. Are parents always considered before their children are?
  2. Are siblings considered in the same order they are displayed in the scene graph?
  3. Is Script Execution Order enforced only in scope of a single GameObject, or does it consider all GameObjects?
1
I was to tell you that you can easily get your answer with a basic setup of few objects, scripts and children, then I realised you posted the answer with the question. Now comes my question, since this is a trivial problem with a fairly easy answer, why asking? - Everts
It's a problem that's been haunting me for years and now that I finally went down and tested it, I thought it might be worth sharing as I was not able to find any documented answer on SO. I'm sure it's caused a lot of people headaches before, especially the fact that the order can change on reloading the scene. Like "it worked when I closed it last night, why isn't it working today?" - Thomas Hilbert
I would say, in the case you need object A to happen before object B, you have a relation, so you cannot rely on Unity update system anymore and A has to call B. You said it yourself, reloading may change the order, so knowing that it could be wrong next time means that if you keep relying on Unity Update knowing it will fail one day, you're doing it wruuuung. - Everts
Yes, that's pretty much the conclusion that comes from the Answer I gave. Before I had the answer I could not draw that conclusion. Unity guarantees that all UI elements are drawn in the order that is defined by the scene graph: siblings in order of display in the hierarchy and children after parents. Also, GameObjects form a scene graph, so their transforms can not be calculated before their parents' transforms are. I just naturally assumed that script execution followed the same rule. - Thomas Hilbert
You cannot program, so that it matters. - Fattie

1 Answers

5
votes

I built a small test project in Unity which basically consists of a 3x3x3 tree of GameObjects, each having 3 scripts.

I found the following answers:

  1. No. Some GameObjects can be considered before their parents are, while some parents can be considered before their children are. And this order can change when reloading the scene or manipulating the scene graph.
  2. No. Siblings can be updated in any order. This order can change when reloading the scene or manipulating the scene graph.
  3. It is enforced over all GameObjects in the scene. If SEO sets script A to be executed before script B, then all instances of script A will be considered before any instance of script B is. Meaning, all instances of A call their Awake() before any B call their Awake(), then all instances of A call their Start() before any instance of B call their Start() and so on.