In Unity, is Awake () called before the start of the game or before the start of the scene?

I read this in the Unity documentation:

Awake is called when an instance of a script is loaded.

Awake is used to initialize any variables or game state before the game begins.

Isn't that contrary? Is the Awake () function called when the game starts, or is it called when the scene loads, where is the script present?

+5
source share
1 answer

Not really if you understand the Execution Order of Events in Unity3D.

A game may contain one scene or several.

So, if the scene is loaded, the sequence:

  • Awake() - Ideal for initializing variables. Also note that if GameObject is inactive during the start of Awake, the Awake method will not be called until this object becomes active (thanks @Everts).

  • Start() - Run your main game logic, because all other Awake() methods of the active GameObject called.

  • First Update() Call, etc ...

What you need to clarify is the term:

.. before the game .

It's easy before your game logic begins.

+5
source

Source: https://habr.com/ru/post/1263311/


All Articles