Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awake() and Start()

Tags:

unity3d

I see that we can initialize Variable in Awake() or Start() and Awake() will be called before Start().

When should we initialize in Awake and Start to have the best performance?

like image 974
mdtuyen Avatar asked Jan 07 '16 09:01

mdtuyen


People also ask

What is the difference between awake () and start ()?

Start is called on the frame when a script is enabled just before any of the Update methods is called the first time. Like the Awake function, Start is called exactly once in the lifetime of the script. However, Awake is called when the script object is initialised, regardless of whether or not the script is enabled.

What is awake () in Unity?

Awake is used to initialize any variables or game state before the game starts. Awake is called only once during the lifetime of the script instance. Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.

Does awake or start Unity?

Use Awake to initialize variables or states before the application starts. Unity calls Awake 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 Awake will be called again.

What does the start function do in Unity?

The Start function can be defined as a Coroutine, which allows Start to suspend its execution (yield).


2 Answers

Usually Awake() is used to initialize if certain values or script are dependent on each other and would cause errors if one of them is initialized too late (awake runs before the game starts). Awake is also called only once for every script instance.

Let me quote the Documentation:

[...] Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.FindWithTag. Each GameObject's Awake is called in a random order between objects. Because of this, you should use Awake to set up references between scripts, and use Start() to pass any information back and forth. Awake is always called before any Start functions. This allows you to order initialization of scripts. Awake can not act as a coroutine.

and about Start():

Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.

Like the Awake function, Start is called exactly once in the lifetime of the script. However, Awake is called when the script object is initialised, regardless of whether or not the script is enabled. Start may not be called on the same frame as Awake if the script is not enabled at initialisation time.

Where the last part makes one big difference

To get to your question:

If the script is NOT enabled at the beginning of your game, and you don't need the variables to be initialized, start would be saving performance as awake() would be called regardless...
every variable would be initialized at the very beginning. At least that's the logical assumption I make.

like image 114
Minzkraut Avatar answered Sep 23 '22 13:09

Minzkraut


This topic is well described in the official docmentation (Awake and Start).

This section describes why you might need two functions:

The Awake function is called on all objects in the scene before any object's Start function is called. This fact is useful in cases where object A's initialisation code needs to rely on object B's already being initialised; B's initialisation should be done in Awake while A's should be done in Start.

The difference between Awake and Start is that Start is called only when a script is enabled.

These two functions are called before the first Update method and there is no performance difference between them. I would say that Awake is used to initialize all objects (like a constructor), and Start is used to link the objects or do something before a game starts.

like image 40
Sergii Zhevzhyk Avatar answered Sep 20 '22 13:09

Sergii Zhevzhyk