Stop countdown function

Hello Everyone!

I have a problem with my code and I can’t find the right workaround.

Here is the simplified code:

function OnMouseDown () {

		CounterEnabled = false;
		n = Random.Range(1.0,6.0);	
		yield WaitForSeconds(n);
		Move(); // This is a round ending with collision

function OnTriggerEnter(other : Collider) 
		if (ButtonPressed == true) 			

function Win ()
		score ++;
		CounterEnabled == true  		 		
function countdown () 
	yield WaitForSeconds(3);

	if (CounterEnabled == true) 


Every round is starting at a random time. If the button is pressed when the collision happens the game is over. To make the game more tense i made a countdown from 3 to 0. It’s starting before each round and the player has 3 seconds to start the round (press the button) or the game is over.

The problem is that even if the round is started, the countdown function is running in the backgound and if the random number is small like 1 second, the round will end before the countdown ends in the background and if the round ends with a Win() it will set “CounterEnabled” to “true” so the player will win and lose at the same time.

However, it needs to be set to true or the next countdown won’t make any sense.
It’s working great if the random range is 3-6 instead 1-6 but i dont want to do that.

Is there a way to stop the countdown function if a round started?

Thank You!

You have a syntax error at line 22 in the function Win, it’s not an assignment but a check statement. It should be

CounterEnabled = true;

Ok, it’s working now.
I used a While loop with a switch statement inside, so i can stop the loop with a variable.