Search Unity

How to "pause" a coroutine ?

Discussion in 'Scripting' started by BooBi, Dec 6, 2012.

  1. BooBi

    BooBi

    Joined:
    Jan 18, 2010
    Posts:
    534
    Hi,

    On my game cut scenes, I've use a SceneManager() Coroutine in which I use something like:
    Code (csharp):
    1.  
    2. //SCENE VILLAGE
    3.         Cam[0].enabled = true;
    4.         Cam[1].enabled = false;
    5.         SceneRoot[0].animation.clip = ACCSIntro[0];
    6.         SceneRoot[0].animation.Play();
    7.         //yield return new WaitForSeconds(0.5f);
    8.         CS_Intro_SoundSystem.SoundOn[0] = true;
    9.         yield return new WaitForSeconds(12f);
    10.         Finger.SetActiveRecursively(false);
    11.         yield return new WaitForSeconds(ACCSIntro[0].length-12f);
    12.         SceneRoot[0].animation.clip = ACCSIntro[1];
    13.         SceneRoot[0].animation.Play();
    14.         yield return new WaitForSeconds(ACCSIntro[1].length);
    15.         SceneRoot[0].animation.clip = ACCSIntro[2];
    16.         SceneRoot[0].animation.Play();
    17.         yield return new WaitForSeconds(ACCSIntro[2].length);
    18.         Girl[0].SetActiveRecursively(true);
    19.         Girl[1].SetActiveRecursively(false);
    20.         SceneRoot[0].animation.clip = ACCSIntro[3];
    21.         SceneRoot[0].animation.Play();
    22.         yield return new WaitForSeconds(ACCSIntro[3].length-1f);
    23.  
    Basically a series of :

    SceneRoot[0].animation.clip = ACCSIntro[0];
    SceneRoot[0].animation.Play();
    yield return new WaitForSeconds(ACCSIntro[0].length);

    I'm calling that Coroutine in the Start() function
    And I found it pretty much straight forward and easy to use.

    Here, the case is slightly different, I want to use that same technique, but the user will touch the screen to move on to the next part
    so something like that :

    SceneRoot[0].animation.clip = ACCSIntro[0];
    SceneRoot[0].animation.Play();
    yield return new WaitForSeconds(ACCSIntro[0].length-5);
    NextButtonOnOff = true;

    if(Next) -> SceneRoot[0]....

    is there a way to sort of Waiting for this next bool to be true to continue in the coroutine? I could do it with a lot of "if" and recall all the time the coroutine, but I'm looking for a clean way of doing it.
    Basically how to pause a coroutine until bool returned (button pressed).

    Thanks for the help
     
  2. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Code (csharp):
    1.  
    2. while(!some_bool_or_condition) yield return null;
    3.  
     
    J1wan likes this.
  3. BooBi

    BooBi

    Joined:
    Jan 18, 2010
    Posts:
    534
    thanks a lot works as I wanted!
     
  4. moh05

    moh05

    Joined:
    Nov 26, 2015
    Posts:
    65
    What if Coroutine is waiting for 1 sec, then it reached 0.4 sec ( thus needs to wait 0.6 more).

    At this specific time, i need the coroutine to pause, then recontinue counting the second ( or the remaining 0.6)

    Is that feasible?
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    yeah:

    Code (csharp):
    1. float timer = 0f;
    2. while(timer < 1f) {
    3.     while(!some_condition) {
    4.         yield return null;
    5.     }
    6.  
    7.     timer += Time.deltaTime;
    8.     yield return null;
    9. }
     
    RT_dev_64 and MGallo2034 like this.
  6. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Snory and J1wan like this.