user5455540
user5455540

Reputation:

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

I read this in Unity documentation:

Awake is called when the script instance is being loaded.

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

Isn't this contradictory? Is the Awake() function called when the game is launched, or is it called when the scene where the script is present is loaded?

Upvotes: 7

Views: 5327

Answers (1)

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

A game can contain a single Scene or multiple.

So, if a Scene is loaded the sequence is:

  • Awake() - Perfect for initializing variables. Also consider if a GameObject is inactive during start up Awake, it's Awake method will be not called until this object is made active (Thanks @Everts).

  • Start() - Start your core Game Logic, because all other Awake() methods of active GameObject's are called.

  • First Update() Call and so on....

What you have to clarify is the term:

..before the game starts..

This means simply, before your game logic starts.

Upvotes: 8

Related Questions