Timer issues, "==" not working!

Heya again Unity Answers, I must be doing something silly again because…

I want bad guys to spawn at set times so I start off with a simple timer -

var myTimer : float = 0.0;

function Update () {
  
 myTimer += Time.deltaTime;
 }

(Source - Unity 3D Student - 3d Modelling)

So now I want it too send a message too the Bad Guy SpawnPoint at set times I put in so as a test —>

var Wave1 : float = 10.0;
var myTimer : float = 0.0;

function Update () {
  
 myTimer += Time.deltaTime;
 }

  if(myTimer == Wave1){
   gameObject.Find("SpawnPointH").SendMessage("turnon");
  Debug.Log("WAVE1");
 }

Yet it won’t print/spawn anything when the timer hits 10. The only way I can get anything to happen is if I set “if(myTimer <= Wave1)” so that they’ll spawn and print the moment the game starts (no good!)

Even stripping the code down to the basic idea…

var myTimer : float = 0.0;
function Update () {
  
myTimer += Time.deltaTime;
 
 }
 if(myTimer == 10){
 Debug.Log("WAVE1");
 }

It still won’t print at 10 seconds :frowning:

Any help would be great! thanks!

  • MSB

What about a simple InvokeRepeating?

var frequency:float;
var spawn :GameObject;
function Start(){
    InvokeRepeating("SendingMessage",0.001f frequency);
}

function SendingMessage(){
   spawn.turnon();
   Debug.Log("WAVE1");
}

Note that I used a reference to the game object instead of finding it each time. Just a little optimization won’t hurt so bad.

You are so very close, but there are a few basic errors.

All code (except declaring global variables) should be encapsulated in parenthesis. There are some other cases, but let’s keep it simple =]

if(myTimer == 10){
    Debug.Log("WAVE1");
}

currently this is living outside of a function.

so

function DoStuff()
{
    // all code goes here !
}

now with your timer : it will be highly unlikely your float will ever be equal to what you want. This is where you were close. Check if it is greater than or equal to a value :

var Wave1 : float = 10.0;
var myTimer : float = 0.0;

function Update () {
	myTimer += Time.deltaTime;
	if(myTimer >= Wave1){
		Debug.Log("WAVE1");
	}
}

Edit :

As was rightly pointed out, this will keep repeating once the waveTime is reached. for multiple waves, the logic will have to be more advanced. For this I suggest using an array to store the time for each wave, though this is probably going to be confusing for the new user.

var waveTimes : float[]; // array to store wave times

private var currentWave : int = 0; // what is the current wave (index, the position in the array)
private var stopSpawning : boolean = false; // set to true when there are no more waves
private var myTimer : float = 0.0; // good ol timer variable

function Start() 
{
	// you can set the values in the waveTimes array in the inspector, or in code
	if ( waveTimes.Length <= 1 ) // if there are no times assigned in the inspector
	{
		waveTimes = new float[ 4 ]; // 4 waves
		waveTimes[ 0 ] = 10.0; // first wave 10 seconds
		waveTimes[ 1 ] = 40.0; // second wave .. you get the idea
		waveTimes[ 2 ] = 70.0;
		waveTimes[ 3 ] = 100.0;
	}
}

function Update() 
{
	myTimer += Time.deltaTime; // increment timer
	
	// check if is supposed to be spawning, or have run out of waves
	if ( !stopSpawning ) // this is thesame as writing -> if ( stopSpawning == false )
	{
		if ( myTimer >= waveTimes[ currentWave ] ) // eg if currentWave is 0, this checks if myTimer > waveTimes[ 0 ]
		{
			Debug.Log("WAVE " + (currentWave + 1) + " HAS STARTED" ); // +1 because arrays start counting at zero, just to show the first wave as Wave1, even though in the array it is Wave0
			
			// set the current wave to the next time
			currentWave ++;
			
			// check if that was the last wave
			if ( currentWave >= waveTimes.Length )
			{
				stopSpawning = true; // umm, stop spawning !
			}
		}
	}
}

Having said all that, you may want to check that the last NPC destroyed before starting the next wave. If so, that would be a different setup. I can expand further if so. Happy Coding =]