- Home /
Two coroutines in Start(), must exec consecutively
I need to run two coroutines in my Start(), and I need them to execute consecutively. Here's my code:
void Start () {
CreateWalls();
StartCoroutine (GenerateMaze(0,0,0));
StartCoroutine (GenerateMaze(24,24,1));
}
To clarify, I want the first GenerateMaze to run. As soon as that function finishes, the second GenerateMaze should execute. The code posted above runs both instances of GenerateMaze on scene play. I tried implementing a spin lock by adding a global bool "ready"
void Start () {
CreateWalls();
StartCoroutine (GenerateMaze(0,0,0));
while (!ready){}
StartCoroutine (GenerateMaze(24,24,1));
}
Yet this causes Unity to crash when it shouldn't. What is the correct way to implement this?
A unity spin lock looks like while(!done) { yield return 0; }
.
Answer by Bunny83 · Apr 19, 2013 at 07:02 AM
You simply need to make Start also a coroutine so it can wait for your other coroutines:
IEnumerator Start ()
{
CreateWalls();
yield return StartCoroutine (GenerateMaze(0,0,0));
yield return StartCoroutine (GenerateMaze(24,24,1));
}
This will execute them one by one. The second yield return wouldn't be necessary since there's nothing after your second GenerateMaze, but just in case... ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613100619im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Coroutines and states 1 Answer
move inside a coroutine 2 Answers
Had difficulties implementing intro to Coroutines from unitypatterns.com. Help? 1 Answer
Can I run multiple instances of the same coroutine in parallel? 1 Answer
Can't get while loop to execute more than once in a coroutine before yielding [Solved] 2 Answers