Lesson 8 – Awake() & Start()

You may have noticed that there is a common element living in the beginning of most of our C# scripts within Unity. Mostly you may have noticed the Start() Method living close to the beginning of your class script. This rhythm is by design, and it is one of many built-in Unity Methods provided to us by the Monobehavior class.

So, what do they do?

I want you to think of Start() and Awake() as the beginning to a good book. You can not have a middle or end without the beginning laying down a solid foundation of characters/settings to work with. The same can be said about the Start() and Awake() methods.

Common use of these methods would be to initialize Game Object References, initialize variables, and in general set up your Game Objects with whatever it needs to function appropriately with the rest of the code below.

Details Zab, I need details!

While Start() and Awake() are similar in that they are used to initialize code/objects, they have just enough differences to offer different uses.

Awake()

Awake() is the first in line of these Monobehavior Methods. It will always be called first before Start() and Update(). And unlike Start(), it will be called even if the script component is not enabled in the Editor. This is great if you want to enable certain code, but don’t want to initialize the script component entirely.

For example, let’s say you have an Enemy Swordsman in your fantasy game that is only meant to get hostile towards you if you harass it by, say, throwing a coconut at its face. Maybe you want to use Awake() to equip a sword on the Enemy Swordsman, but do not want to enable the script to activate all the other behaviors unless he is indeed harassed…with a coconut to the face (What a ridiculous scenario AND a very loose example — but it gets the point across, right?).

Lastly, best-practice for Awake() has been detailed in the Unity Documentation site as follows: “…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.” In short, use Awake() to initialize Game Objects before Start(), Update(), and the like.

Example of initializing a Game Object with Awake:

using UnityEngine;
using System.Collections;

public class ExampleClass : Monobehavior
{
    private GameObject Enemy; // we make a variable for the object

    Awake()
    {
        /* we then assign the variable the Object Reference in the Awake before
        the script is even enabled. */
        enemy = GameObject.FindWithTag("Enemy");
    }

}

Start()

Start() is called right after Awake(), but before Update(). It behaves very similarly to Awake(), with the caveat that the Script component must be enabled on the GameObject its attached to in order to be called. This is useful because you can use Awake to initialize, and delay the entire Script being active until you really need it (e.g. Please see the example used in Awake as a reference).

Conclusion:

Initializing your Game Objects properly is highly important when scripting in Unity, and I imagine the concept is of importance in most programming languages. I already had a basic understanding of how to use Start(), but knowing the deeper details of the order and function will give be valuable to me in the coming months.

Study Resource:
https://unity3d.com/learn/tutorials/topics/scripting/awake-and-start?playlist=17117

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s