Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 805
BenjaFriend Avatar asked Apr 24 '26 02:04

BenjaFriend


1 Answers

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");
}
like image 200
Programmer Avatar answered Apr 25 '26 17:04

Programmer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!