- Home /
Debug.Log print order, or Awake vs Start issue?
This seems crazy but I need to confirm it.
Does Debug.Log guarantee any kind of order that the messages are displayed relative to when Debug.Log() is called? I'm seeing some cases where it seems they're being shown out of order.
For example:
// Instantiate object
myObj = (GameObject) MonoBehaviour.Instantiate(somePrefab);
myObj.Foo();
Then within SomePrefab,
void Awake() { Debug.Log("Awake called"); }
void Start() { Debug.Log("Start called"); }
void Foo() { Debug.Log("Foo called"); }
The output is:
Awake called
Foo called
Start called
I am surprised that Foo() is called even before SomePrefab's fake constructor Start() thing has finished. Is it a Start()/Awake()/Instantiate issue, or a Debug.Log issue?
Answer by Loius · May 10, 2013 at 06:24 AM
Nope, that's the right order. Awake is immediate initialization - essentially you're saying 'this init doesn't rely on anything, but things might rely on what i do here' inside an Awake. Start is 'i might need some data from somewhere else', so it waits its turn. As far as I know they happen within the same frame (awakes are called instantly, starts are piled up and executed one after the other at the end of all the normal unity events).
If you can call methods on an object before its Start() is called, when would you ever want to put stuff in Start() ins$$anonymous$$d of Awake()? It seems like asking for things to be undefined.
It seems the first Update() isn't called until after Start(), so you could put things in Start() that you only need in Update(), but it seems like asking for trouble.
Start is called just before the object is actually displayed for the first time.
Start is very useful because it lets you wait until an object is ready to be used before playing music, acquiring resource etc. It can also be a coroutine which is really handy. Using Start you can $$anonymous$$imise memory and processing impact of things that never become active too.
$$anonymous$$any of my scripts have just a coroutine Start function, or an Awake and Start.
Oh yeah and without messing about with script execution orders Start can be used to find other fully configured objects.
"It seems like asking for things to be undefined."
@ben, it can seem like that when you're just starting with Unity or a frame based game engine. When you are comfortable with it you will completely understand the difference between the two. A game engine is not possible without the distinction between the two.
Note that, quite simply, on Unity's documentation there is an excellent and long explanation of the difference.
or you can always have me being a smartass, Loius being a bitter smartass, and Whydoi being sage :-)
one simple tip, every script has hundreds of "GameObject.Find(blah).GetComponent(blah)" statements. generally you have to do those in Awake(), so they are all available. then you do initialization (using those) in Start.
Your answer
Follow this Question
Related Questions
Why wouldn't Debug.Log show up in Start or Awake ? 3 Answers
Debug.Log is causing an Assert 2 Answers
DontDestroyOnLoad() not calling awake/start again 2 Answers
Object reference becomes null between Awake and Start after scene load 2 Answers
does finction start or awake run when the object or script is enabled mid game 3 Answers