- Home /
Prevent update until child script start completes?
I have a script A that adds another script B:
function Start () {
gameObject.AddComponent.<B>();
}
And then makes a general check for activity:
function Update () {
if (!IsActive()) {
// Code that says we are done...
}
}
The problem appears to be that having added B, the Update method of A is called before B has started. As a result, A immediately decides that there is no activity when, in fact, A still needs to wait for B to start and complete.
What is the best way to handle this kind of inter script coordination? Should I add B in a function invoked from A's Start and call yield?
Answer by Jamora · Jul 10, 2013 at 05:58 AM
Awake is always called when the script loads, i.e. right after its creation. You set up all references and values there, much like in a constructor. Start is called right before the first Update for that script.
Instead of Start in your scripts, you could consider Awake, if all that needs be done is set up values.
Thanks will give that a try. I did find that using yield in my start method does work but I prefer your suggestion.
$$anonymous$$oved to use Awake and works perfectly, thanks.
Your answer
Follow this Question
Related Questions
Question about Unity documentation GameObject.Find 2 Answers
Is it absolutely unconditionally true that Start runs BEFORE the FIRST Update? 3 Answers
Failed to start unity package manager. Version 2017.2 1 Answer
Probably Dumb Question: Update(), Start(), etc. Don't Work Inside Classes (JavaScript)...? 2 Answers
Can the Script Execution order be adjusted for FixedUpdate()? 3 Answers