Awake()

I have read the unity scripting reference i understood the things said until i came across this part

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.

Can someone show me a good example of what this is saying?

Can’t depend on Awake() to do things in any kind of sequence.

Use it to initialize internal values in your script like dictionaries, lists, etc… which is useful because it always runs before Start and Update so you know they’ll be ready and initialized.

Yes, i understand Awake() runs before start and Update().

Its saying don’t to inter-script communication in Awake.

1 Like

can you please show me example of a reference set up in awake() please.

They are saying do not attempt to communicate with other scripts in Awake(), do that in Start().

An example of setting up a reference in Awake():

private Text textAttachedToThisGameObject;

void Awake()
{
    //
    // Get a reference to something attached to THIS game object
    //
    textAttachedToThisGameObject = GetComponent<Text>();
}

If you want to get a reference to another game object.

private GameObject someOtherGameObject;

void Awake()
{
    //
    // Get a reference to a different game object.
    //
    // Note: Find will not find inactive objects (ie if enabled == false)
    //
    someOtherGameObject = GameObject.Find("someRandomGameObject");
    //
    // DO NOT ASSUME anything about this object, it may or may not be initialised yet.
    //
}

void Start()
{
    //
    // Use the reference to a different game object
    //
    someOtherGameObject.transform = doFunkyStuffToATransform(someOtherGameObject.transform);
}

You should probably take a look here: http://unity3d.com/learn

1 Like
MyScript myScript

Awake (){
    // This line is fine
    MyScript = GetComponent<MyScript>();
    // This line might cause issues
    myScript.someMember.SomeMethod();
}

Your second example is using GameObject.Find as an example but in the reference it states that Awake() is called after all gameObjects are initialized so there should be no problem there i am guessing.

As far as I understand the objects are all created/instantiated prior to Awake being called on any (though I can not find any documentation to support this) In practice this assumption has held true for me.

Edit: As pointed out by @Kiwasi and @matriXcel it is actually documented clearly right here: MonoBehaviour.Awake.html

For a detailed description of the lifecycle/execution order see this reference: Execution Order

But to remove any chance of issues I see no reason to not do both steps in Start() rather than in Awake() unless there is a pressing need.

It’s actually a valid assumption. GameObjects in the scene will all be created before Awake is called. They will have references to their respective components. So Find and GetComponent are perfectly valid in Awake.

In short, you can get whatever references you like. But you should avoid using the references until Start.

This all becomes more complicated I you start calling Instantiate from Start or Awake.

2 Likes

Right, I do this myself but having just done a little research I could not find any documentation to support this (so I assumed I was just lucky in the instances where I do this). I did find some posts that claimed “OnEnabled and Awake() are called on objects when they are instantiated” or words to that effect, so then I started doubting my own assumptions (as per my corrections to my posts)…

I’ll amend them back :slight_smile:

Middle of the second paragraph.

1 Like

Excellent, thanks BM.

There is one more thing about Awake() which is in my opinion super important and gave me a lot of headache: Awake() is always called regardless of the Gameobjects active state! Any code in Awake() will be called, even if the Gameobject itself is not active.

1 Like

Are you sure on this? My results show Awake not being called on an inactive object until you activate it. Which is kind of annoying. Ideal behaviour would be calling Awake the moment an object is Instantiated. Like a constructor.

1 Like

I concur. I just tested this with Unity 5.0 and as @Kiwasi states, It does not call Awake() on an inactive object.

Oops I am sorry, I mixed something up BUT I am still not totally wrong, though.
Yes, Awake() will not be called if the holding GameObject is not active, but Awake() will be called on an Active GameObject if the Script is disabled.
Basically: GameObject active, Script disabled = Awake() will still be called.

1 Like

This is all pretty simple once you actually start putting things into practice and can see what each method does and is supposed to be used for. It’s not something you should be overthinking.

1 Like

http://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html

I see you’re right,thank you.