Scripting Reference

◆ OnAwake() [5/5]

virtual void Sisus.Init.MonoBehaviour< TFirstArgument, TSecondArgument, TThirdArgument, TFourthArgument, TFifthArgument >.OnAwake ( )

OnAwake is called when the script instance is being loaded during the Awake event after the Init function has finished.

OnAwake is called either when an active GameObject that contains the script is initialized when a Scene loads, or when a previously inactive GameObject is set active, or after a GameObject created with Object.Instantiate is initialized.

Unity calls OnAwake only once during the lifetime of the script instance. A script's lifetime lasts until the Scene that contains it is unloaded. If the Scene is loaded again, Unity loads the script instance again, so OnAwake will be called again. If the Scene is loaded multiple times additively, Unity loads several script instances, so OnAwake will be called several times (once on each instance).

For active GameObjects placed in a Scene, Unity calls OnAwake after all active GameObjects in the Scene are initialized, so you can safely use methods such as GameObject.FindWithTag to query other GameObjects.

The order that Unity calls each GameObject's Awake (and by extension OnAwake) is not deterministic. Because of this, you should not rely on one GameObject's Awake being called before or after another (for example, you should not assume that a reference assigned by one GameObject's OnAwake will be usable in another GameObject's Awake). Instead, you should use Awake/OnAwake to set up references between scripts, and use Start, which is called after all Awake and OnAwake calls are finished, to pass any information back and forth.

OnAwake is always called before any Start functions. This allows you to order initialization of scripts. OnAwake is called even if the script is a disabled component of an active GameObject. OnAwake can not act as a coroutine.

Note: Use OnAwake instead of the constructor for initialization, as the serialized state of the Component is undefined at construction time.