- Home /
Is there any way to stop WaitForSeconds() ?
Hello,
I recently have an issue, where I want to stop a Coroutine, if the bool secondChanceUsed is equal to true. But I have no idea to do so, because StopCouroutine() will stop the whole coroutine and yield break will not let calling the win() function.
I hope you can help me out with this and thanks in advance!
  IEnumerator countdown(){ yield return new WaitForSeconds(5f); if(!secondChanceUsed){ enableLooseUI(); disablePreLooseUI(); updateTotalPoints(); pointsystem.resetLocalscore(); } else{ // Stop waiting for 5 Seconds and continue with win(); }
maybe put the waitforsecond in the if(!secondchanceused) and change else to if(secondchancedused)..
or put ins$$anonymous$$d of an number in waitforsecond a int’which you can modify in your if statements.
Answer by Hellium · Aug 01, 2018 at 07:18 AM
No, it's not possible to stop a WaitForSeconds only. Here is how to handle your problem:
 IEnumerator countdown()
 {    
     for( float timer = 5 ; timer >= 0 ; timer -= Time.deltaTime )
     {
         if( secondChanceUsed )
         {
             win();
             secondChanceUsed = false;
             yield break ;
         }
         yield return null ;
     }
     enableLooseUI();
     disablePreLooseUI();
     updateTotalPoints();
     pointsystem.resetLocalscore();
 }
What is the point of using IEnumerator then? Why not just doing it in a simple function?
Because a simple function can't wait. The coroutine allows you to wait until 5 seconds have elapsed OR secondChanceUsed turns true. 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                