Function Timing

I’ve been learning a little Unity scripting lately, and I have a little JS script that currently prints a simple debug.log() message to the screen every frame (because it’s called inside the update function, which I know is called every frame). How could I make it so the function only logs a message to the console every few seconds? I tried using yeildwaitforseconds(), but it doesn’t work because the update function is being called every frame.

I figured I could just use some sort of counter, but it seems like the wrong way of doing it for some reason.

Any help would be greatly appreciated… I’m stumped.

You can use yield from method Start() but not from update

Is there any way whatsoever to make something happen at certain intervals, just by scripting?

If you mean in the update, since using a coroutine is scripting, then you would just keep track of time and print the debug when one second has elapsed. You could look at the time functions, but TimeSinceLevelLoad might work.

float nextTime;
void Start(){
nextTime = Time.TimeSinceLevelLoad + 1.0f;

}
void Update(){
if(Time.TimeSinceLevelLoad > nextTime){
Debug.Log("a message");
nextTime = Time.TimeSinceLevelLoad + 1.0f;
}
}

In general, don’t use Update if you don’t want something updated every frame. Aside from using yield in coroutines, a simple way of doing stuff at intervals is to use InvokeRepeating.

–Eric