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();
	}`

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();
}

maybe put the waitforsecond in the if(!secondchanceused) and change else to if(secondchancedused)…

or put instead of an number in waitforsecond a int’which you can modify in your if statements.