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:
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.
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.
Unity Technologies Component order does not matter.
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.
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:
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With