- Home /
Is it absolutely unconditionally true that Start runs BEFORE the FIRST Update?
I appreciate there are numerous questions / doco pages on the various aspects of what-comes-first.
But, is it absolutely true that Start() runs BEFORE the FIRST Update() ?
Is this always the case, even if the gameObject enters the scene inactive, it comes from a prefab, or whatever the hell else happens?
(You could almost ask: IS THERE ANY CASE where the first Update() WILL come prior to Start() ?)
Thanks.
FTR, this in relation to the astounding Jessy Insight here:
I notice if you deactivate then reactivate a script, Start() is not called. It was most inconvenient.
Right, you need (I think) OnEnable() for that - but it's very hard to keep it fresh in your head!
Answer by whydoidoit · Sep 26, 2013 at 12:36 PM
Well Start won't necessarily complete before the first Update, if Start is a coroutine, only the code before the yield will execute before Update, then Update, then Update again, then whatever comes after the yield. Thereafter the code will continue to run after Update.
IEnumerator Start()
{
Debug.Log("Before Update");
yield return null;
Debug.Log("After Update");
yield return null;
while(true)
{
Debug.Log("Start coroutine");
yield return null;
}
}
void Update()
{
Debug.Log("In Update");
}
Also be aware that it is generally a bad idea to use Start as a coroutine if there is any danger your object will be disabled and re-enabled. Ins$$anonymous$$d use OnEnable to start a coroutine.
Right on with the completion aspect. (Indeed, note Jessy's incredible trick in the link - I'd never thought of it.)
You're pretty sure there's no unusual circumstances where Start would not proceed Update ?
(You know how there's a freaking vast number of "corner cases" with this stuff. If it's just been made inactive .. whatever .. loaded .. maybe I'm straight wrong and it's "absolutely bulletproof truth".)
I code as if it were and have never noticed any problem.
"I code as if it were and have never noticed any problem."
Fair Enough.
i think this is the best thing to an answer on this trouble-raising issue! heh
Answer by Visual Programmer · Sep 26, 2013 at 12:31 PM
Start is always called before the first Update (hence why it is called start). You can even test if for yourself by putting in Debug.Log("This is start") and Debug.Log("This is update") in each function.
O$$anonymous$$. But would you bet your life on this knowledge?
No need to bet my life. It's how it works. Start is always called before, but as whydoidoit mentioned, you can delay the completion of the function.