StartCoroutine() Not working for me

Hello,
So I am trying to pause a page before starting new level and I am curious if you can have two StartCouroutine() in same if statement.

When score is reached we run the following

        //When score reaches certain level, the level will be changed
        if (score >= 50 && level == 1)
        {
            Debug.Log("Fist Level Completed"); //This shows in Console

            level++;
            SceneManager.LoadScene("Level2");
            DontDestroyOnLoad(this);
            StartCoroutine(CompletedLevel());  
            StartCoroutine(Levels());
                        
        }

The StartCoroutine(CompletedLevel()); is below:

    //Completing level
    IEnumerator CompletedLevel()
    {
        int displaylevel = level - 1;
        Debug.Log("CompletedLevel Called");  //This shows in Console
        LevelStart.text = "Congratulations You finished level " + displaylevel; //This does not show on screen
        Debug.Log(LevelStart.text);  //This shows text from Console
        yield return new WaitForSeconds(15); //There is no pause
       
    }

Then the following runs StartCoroutine(Levels()); see below:

    //This displays message for each level before game starts
    IEnumerator Levels()
    {
       
        LevelStart.text = "Level " + level + " - Get Ready!!!";
        yield return new WaitForSeconds(5);
        LevelStart.text = "";
        hazardCount = 10 + (level * 5);
    }

For some reason its bypassing the 15 second pause in StartCoroutine(CompletedLevel()), can someone point me in the right direction.

dont think you understand how coroutines work, just because a coroutine is running that does not mean it block executing of the next line of code.

you would have to start your 2nd coroutine inside of your first one instead

1 Like

You are correct, apparently I do not. I thought that when you put the wait 15 seconds it would pause during that time. So a better way may be to use a Time.timeScale = 0 to pause for a certain amount of time?

Thanks for your response.

time scale would not make functions pause, all it would do is slow down animations, physics and anything multiplied by delta time.

just nest the coroutines or make some sort of manager for firing them off sequentially. A other good option could be add a onComplete delegate to your coroutine.

havent tested it, but this should be a easy way to chain coroutines using delagates

using System.Collections;
using UnityEngine;
using System;

public class CoRoutineTest : MonoBehaviour {
    private void Start() {
        StartCoroutine(Routine01(() => {
            StartCoroutine(Routine02(() => {
                StartCoroutine(Routine03());
            }));
        }));
    }

    private IEnumerator Routine01(Action onComplete = null) {
        // do stuff here
        yield return new WaitForSeconds(5f);
        if (onComplete != null) {
            onComplete();
        }
    }

    private IEnumerator Routine02(Action onComplete = null) {
        // do stuff here
        yield return new WaitForSeconds(2f);
        if (onComplete != null) {
            onComplete();
        }
    }

    private IEnumerator Routine03(Action onComplete = null) {
        // do stuff here
        yield return new WaitForSeconds(5f);
        if (onComplete != null) {
            onComplete();
        }
    }
}

A other good option is to just execute all of your coroutines in a coroutine. The Coroutines you yield on just like you do with WaitForSeconds so something like this.

using System.Collections;
using UnityEngine;

public class CoRoutineTest : MonoBehaviour {
    private void Start() {
        StartCoroutine(MasterRoutine());
    }

    private IEnumerator MasterRoutine() {
        yield return StartCoroutine(Routine01());
        yield return StartCoroutine(Routine02());
        yield return StartCoroutine(Routine03());
    }

    private IEnumerator Routine01() {
        Debug.Log("do stuff here");
        yield return new WaitForSeconds(5f);
    }

    private IEnumerator Routine02() {
        Debug.Log("do stuff here");
        yield return new WaitForSeconds(2f);
    }

    private IEnumerator Routine03() {
        Debug.Log("do stuff here");
        yield return new WaitForSeconds(5f);
    }
}

I am going to mess around with the Routines, what I did get to work is create as sub, stop the Coroutine that created the enemies, and then restart after showing summary of level completed, seems to work well for my simple game. But going to take a look at the code above. Thanks for the feedback. I like the idea of OnComplete to make sure its done.

Thanks again.