Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Stopping a "yield return ..." coroutine.

Discussion in 'Scripting' started by katsuragi545, Mar 20, 2015.

  1. katsuragi545

    katsuragi545

    Joined:
    Jun 25, 2014
    Posts:
    38
    I have a coroutine that play's a List of audioclips together:
    Code (CSharp):
    1. public IEnumerator PlayAudioSequence(List<AudioClip> aClipsList )
    2. {
    3.     if(aClipsList.Count == 0)
    4.         yield break; //empty
    5.    
    6.     for(int i = 0; i < aClipsList.Count; i++)
    7.     {
    8.         audio.clip = aClipsList[i].Audio();
    9.         audio.Play();
    10.         yield return new WaitForSeconds(aClipsList[i].Audio().length);
    11.     }
    12. }
    Here's the problem, I am calling this coroutine from another script using:
    yield return vpc.StartCoroutine("PlayAudioSequence");
    Here, vpc is a reference to the script containing the function above.

    I've implemented a skip button that allows the player to skip to the next scene instead of listening to the list of audio clips - I'm unable to stop the coroutine though. It seems possible to stop coroutines that aren't called using "yield return ...". Does anyone know how to stop these type of coroutines? I've used StopAllCoroutines and StopCoroutine and nothing works.
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Code (CSharp):
    1. yield break;
    Should stop a coroutine.
     
  3. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    If you want to stop it from outside, then StopAllCorountines should work, as should StopCoroutine("CoroutineName"); Note that this will only work if you started the Coroutine with StartCoroutine("CoroutineName").
     
    sergeyrar likes this.