"Two coroutines in Start(), must exec consecutively" (502780)

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?

Why not just run the second one FROM the first one as a last step?

Also - you would need to yield within that while loop.

I tried that before implementing a spinner, but I was getting irregular results. I assumed it was weird behavior caused by nested coroutine calls, but it was actually a bug in my code. Got it working thanks :smile:

Starting two coroutines means they will be executing along side each other. That is the nature of a coroutine.

It sounds like you have it working, but why do you need coroutines at all? If you call two functions, one after each other, the first function will finish, then the second will run.

Despite me dont really getting the idea behind that too, you could make your start function a coroutine liek tihs:

IEnumerator Start() {
 yield return StartCoroutine(GenerateMaze(0,0,0));
 yield return StartCoroutine(GenerateMaze(24,24,24));
}

This code will wait for the first coroutine to finish then start the second and then wait for the second to fnish. Sense might be missing though.