Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GameObject update order in Unity

Tags:

unity3d

unity5

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?
like image 338
Thomas Hilbert Avatar asked Mar 27 '16 20:03

Thomas Hilbert


People also ask

Is OnEnable or awake called first?

Awake: This function is always called before any Start functions and also just after a prefab is instantiated. (If a GameObject is inactive during start up Awake is not called until it is made active.) OnEnable: (only called if the Object is active): This function is called just after the object is enabled.

How do I change the order of scripts in Unity?

But you can set Script Execution Order in the settings (menu: Edit > Project Settings > Script Execution Order) or change it from code: Script Execution Order manipulation. Changing Unity Scripts Execution Order from Code.

Does order of components matter Unity?

Unity Technologies Component order does not matter.

What does update () do in Unity?

Update is called every frame, if the MonoBehaviour is enabled. Update is the most commonly used function to implement any kind of game script. Not every MonoBehaviour script needs Update . using UnityEngine; using System.


1 Answers

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.
like image 75
Thomas Hilbert Avatar answered Oct 23 '22 04:10

Thomas Hilbert