CountDown Help

Basically, I need to make a countdown that corresponds with the wait time in between waves. Currently once a wave finished there is a wait time before the next one starts. I want to make a counter that is displayed so i can visually show the wait time. ex. “Next Wave in: 5…4…3…2…1”. The only variables i really have is the waveWaitTime and a variable i set to that waveWaitTime to then decrement as the timer.

Here is the UpdateWave function

function UpdateWave()
{
	
	yield WaitForSeconds(waveWaitTime);
	
	if(updatingWave == true)
	{
		I++;
		waveNumber++;
		SpawnEnemies(waves[I]);
		updatingWave = false;
	}

}

Here is the proposed timer function

function WaveTimer()
{
	//On function Start() var m is set to waveWaitTime which is 5.
		while(m > -1)
		{
			yield WaitForSeconds(1);
			m--;
		}
	
}

Problem is that when i call this function anywhere in the Update function it decrements m like a million times a second. I need to make it so m is decremented at the same time the game is yielding for the waveWaitTime.

I think you have to call it as a coroutine if it’s in update.
http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

still cant get it to work. I it keeps decrementing the timer by 1 every frame to it shoots into the negative almost instantaneously. I need to to decrement at the same rate the game is waiting to start the next wave. So if the game is waiting 5 seconds. I need the counter which is 5 to decrement every second at the same time the game is waiting.

When game starts waiting…

InvokeReapting(“functionName”, 0.1, 1.0);

Okay so i got it so it decrements correctly however there is now no actual wait before the wave starts. The timer runs correctly though. here is the code i have so far.

UpdateWave function

function UpdateWave()
{
	InvokeRepeating("CountDown", 0.1, 1.0);
	
	
	if(updatingWave == true)
	{
		I++;
		waveNumber++;
		SpawnEnemies(waves[I]);
		updatingWave = false;
	}

}

CountDown function

function CountDown()
{
	
	//yield WaitForSeconds(1);
	m--;
	
	if(m <= 0)
	{
		doneCountdown = true;
		CancelInvoke();
		
	}
		
}

Update function

function Update () 
{
	
	if(enemyCounter == 5  !isSpawning || enemyCounter == 12  !isSpawning)
	{
		updatingWave = true;
		UpdateWave();
	}
}

I need a way to flag the updating of the wave after i Invoke that method without screwing up the counter.

how does
“updatingWave”
become false?

edit*

if UpdateWave(); is what creates the wave and you say there is no delay between the wave ending and beginning then the conditions you set …

if(enemyCounter == 5 !isSpawning || enemyCounter == 12 !isSpawning)

are somehow faulty. What I mean by faulty is that they are true when the should be false. And if that is not the case, you need to set one more condition ex. createWave : boolean. if true, then you can create wave and not before. Set it to true via the “CountDown()”. When your count hits 0, createWave = true. Your Update picks that up, and you create your wave and set createWave to false, so that it does not create another wave until you insruct it.

Kay so i figured out a rather rudimentary and rough way of doing the counter but it works. Sometimes there is a slight glitch where the counting speeds up but that may vary depending on the computer you are using.

FIXED AND WORKING CODE:

function UpdateWave()
{
	
	for(m = waveWaitTime; m > -1; m--)
	{
		if(doneCountdown == false)
			yield WaitForSeconds(1);		
	}
		
	ResetM();
		
	
		
	if(updatingWave == true)
	{
		I++;
		waveNumber++;
		SpawnEnemies(waves[I]);
		updatingWave = false;
		doneCountdown = true;
	}

}