BenjaFriend
BenjaFriend

Reputation: 664

Unity StopCoroutine not working

I have a method to start a coroutine, and should stop it and reset some things before starting it again.

private Coroutine wholeTutorialRoutine;

public void RunWholeTutorial()
{
    tutorialText.text = "";
    StopAllCoroutines();

    if(wholeTutorialRoutine != null)
    {
        StopCoroutine(wholeTutorialRoutine);
    }

    wholeTutorialRoutine = StartCoroutine(WholeTutorial());

}

private IEnumerator WholeTutorial()
{
    // Wait until after we are done showing this dialouge
    yield return StartCoroutine(ShowDialougForSeconds("tap_to_kill", 5f));

    yield return new WaitForSeconds(5f);

    yield return StartCoroutine(ShowDialougForSeconds("larger_enemies", 5f));

    yield return new WaitForSeconds(5f);

    yield return StartCoroutine(ShowDialougForSeconds("press_button", 7f));

    yield return new WaitForSeconds(3f);

    yield return StartCoroutine(ShowDialougForSeconds("button_colors", 5f));
}

private IEnumerator ShowDialougForSeconds(string diagID, float time)
{
    SetText(diagID);

    tutorialText.GetComponent<Animator>().SetTrigger("FadeIn");

    yield return new WaitForSeconds(time);

    tutorialText.GetComponent<Animator>().SetTrigger("FadeOut");
}

wholeTutorialRoutine is a private field of type Coroutine.

I feel like there is something funky happening with those WaitForSeconds calls, but I am not quite sure what.

RunWholeTutorial() is hooked up to a button, so I want to stop the current tutorial, and start again if the user presses it over and over again.

What happens currently seems to be that the coroutines run on top of one another.

Upvotes: 3

Views: 9355

Answers (1)

Programmer
Programmer

Reputation: 125305

Use IEnumerator instead of Coroutine to store the instance of the RunWholeTutorial coroutine function once in the Start function. You can then be able to start and stop it with that IEnumerator variable.

private IEnumerator wholeTutorialRoutine;

void Start()
{
    wholeTutorialRoutine = WholeTutorial();
}

public void RunWholeTutorial()
{
    tutorialText.text = "";

    if (wholeTutorialRoutine != null)
    {
        StopCoroutine(wholeTutorialRoutine);
    }

    StartCoroutine(wholeTutorialRoutine);
}

private IEnumerator WholeTutorial()
{
    // Wait until after we are done showing this dialouge
    yield return StartCoroutine(ShowDialougForSeconds("tap_to_kill", 5f));

    yield return new WaitForSeconds(5f);

    yield return StartCoroutine(ShowDialougForSeconds("larger_enemies", 5f));

    yield return new WaitForSeconds(5f);

    yield return StartCoroutine(ShowDialougForSeconds("press_button", 7f));

    yield return new WaitForSeconds(3f);

    yield return StartCoroutine(ShowDialougForSeconds("button_colors", 5f));
}

private IEnumerator ShowDialougForSeconds(string diagID, float time)
{
    SetText(diagID);

    tutorialText.GetComponent<Animator>().SetTrigger("FadeIn");

    yield return new WaitForSeconds(time);

    tutorialText.GetComponent<Animator>().SetTrigger("FadeOut");
}

Upvotes: 3

Related Questions