Help: Every X second do this

Okay, my problem is following:

I can’t seem to get Unity to execute on a defined time. I’ve used the code below, which is very similar to what found on unity’s scripting documentation. So far it print one time at 0, then just rapidly after 5 seconds has passed. What is the thing I’am missing?

Just to clearify it: I want it to print…wait 5 sec…print…wait 5 sec etc.! :slight_smile:

function Update () {
	var everySec = 5.0;
	var nextPrint = 0.0;
	
	if (Time.time > everySec + nextPrint)
	{
    	nextPrint = Time.time;
    	print(everySec);
    }
}

This is the reference code:

// Instantiates a projectile off every 0.5 seconds,
// if the Fire1 button (default is ctrl) is pressed.

var projectile : GameObject;
var fireRate = 0.5;
private var nextFire = 0.0;

function Update () {
    if (Input.GetButton ("Fire1")  Time.time > nextFire) {
        nextFire = Time.time + fireRate;
        var clone = Instantiate (projectile, transform.position, transform.rotation);
    }
}

Sounds like you might actually want to use Invoke rather than trying to do something so precise in Update.

You have nextPrint as a local var. Make it a member var so that its value is stored between frames.

burnumd: Yeah, well perhaps. Will look into that.

Thank you BlackRabbit:) It now works. Better read up on that local vs member;)