Question About Instantiating in Awake

I just updated my Unity version, And I ended up updating the new template scripts and stumbled upon a note in Unity’s Documentation about the Start Method

Where objects are instantiated during gameplay, their Awake function is called after the Start functions of Scene objects have already completed.

This seams to imply that if we Instantiate a Game Object that it’s Awake method will not be called until after the Start methods of everything in the Scene has completed. I am guessing that since it is listed in the Start method that it is only talking about Instantiating Game Objects from within the Start Method.

Does anyone know if this is the case or can we potentially run into an Issue by Instantiating a Game Object in the Awake Method (The Game Object’s Awake Method is not called until after all of the Scene Objects have run their Start Methods)?

My assumption is that Awake is not considered to be part of the “gameplay” cycle, but part of the construction cycle.

Note: Use Awake instead of the constructor for initialization, as the serialized state of the component is undefined at construction time.

I’ve actually not noticed that line before. This is something that should be testable…
In an Awake() method, instantiate a GameObject with a script containing

void Awake()
{
    Debug.Log($"Awake! I Live");
}

In the Start Method of the script that instantiates should contain this method:

void Start()
{
    Debug.Log($"Start: Was I first?");
}

See which one happens first in the console.

1 Like

You are correct this can be tested. For any one else interested. I am using 2021.3.11f1

public class InstantiateTest : MonoBehaviour
{
    public GameObject prefab;

    #region Unity Events

    #region Init

    private void Awake()
    {
        if (!prefab) return;
        Debug.Log($"Instantiating a Game Object from Awake");
        Instantiate(prefab);
    }
    private void Start()
    {
        if (prefab)
        {
            Debug.Log($"Instantiating a Game Object from Start");
            Instantiate(prefab);
        }
        Debug.Log($"Start: Did I Finish before Instantiated Awake?");
    }
	
	#endregion

	#endregion
}
public class InstantiatedTest : MonoBehaviour
{
	#region Unity Events

    #region Init

    private void Awake()
    {
        Debug.Log($"Awake! I am now a live");
    }
	
	#endregion

	#endregion
}
public class SceneObject : MonoBehaviour
{
    #region Unity Events

    #region Init

    private void Awake()
    {
        Debug.Log("Awake: I just live in the Scene");
    }
    private void Start()
    {
        
        Debug.Log("Start: Did I Run before Instantiated Object's Awake");
    }
	
	#endregion

	#endregion
}

Interesting results. Got What I expected for the Instantiate in the Awake, portion. Got Unexpected results with the Start.

Unity_nlP2DGJmwf

I expected the Second Awake Message to be last. I would be interested to know what others got with their specific Unity installation.

Unity_vuicPv4E29

Also flipping the Game Objects in the Hierarchy produced the same results. I would expect someone to possibly have the I Live in the Scene to Display after the Instantiating a Game Object from Awake as there is no way to tell (Guarantee) which Awake would run first in this instance.

I guess that They can only mean stuff Instantiated after the game has started running, the Scene was loaded and Update Loop has already started, they just worded it a little funky

I’m not in a position to test this tonight, but this is actually the behaviour I expected, based on Instantiating things in Awake and being able to rely on their Awakes having run. The comment on the Unity Documentation contradicted my own experience.

This is somewhat what I was expecting, by the wording I was Expecting the Start to finish before the last Awake method ran. I was expecting the Awake Instantiated Object to run it’s Awake before any of the other Start Methods ran. I never tried to rely on something that was Instantiated from the Start Method in another script. I have Instantiated a prefab from an Update or Trigger Event (some place in the Game Loop and got a value from that object right afterward.

I have ran into a scenario in the past where the Documentation completely contradicted itself, It said one thing at the beginning, and then latter stated something totally different. If I remember right it stated that the return type was one thing and then it stated the method returned something totally different, was something to do with the Shader Graph Nodes, how one of them worked. When I was playing around with using Shader Graph and learning how it worked.

I can say that the Documentation has come a long way since Unity first came along. It has greatly Improved, with 1,000 more examples on things, even though some of the Methods do not have examples along with them. Some feature that they have included that was need for some reason, I usually stumble across this when learning something new and say hey look this Class has this Method I not quite sure what to expect from the results of using it lets look at the example, no example ok lets use some debug logs and maybe some break points here and test it out.

Yes a lot fewer entries like:

Foo: See Bar

Bar: See Foo

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms