Using Update loop vs Coroutine for exact timer

It seems that most modern devices operate at close to 60fps which means that Time.deltaTime is very small. Is it a bad idea to use Update as a global timer instead of IEnumerator/Coroutines

This depends on how precise you want the timer to stop. With Update() call and Time.deltaTime, you will always have the overshoot problem, saying you want the timer to stop on 1 second after but you most likely to get something like 1.0038215 seconds.

Edit: They both have this issue, but coroutines will give you a closer result.

I’m not quite sure what you mean by “global timer”, but Coroutines are pretty much universally better than using Update IMO- they can run at the same “once per frame” frequency as Update, but they can also yield for long periods of time without wasting resources being processed every single frame, they can optionally yield multiple times per frame when needed or during different loop timings (the Update loop, the LateUpdate loop, and the FixedUpdate loop), and they can be finished and ended without disabling the rest of the Component.

The only thing that Update really has going for it is that it’s a few seconds faster to implement in test scripts.

My latest game project uses Update, LateUpdate, and FixedUpdate in exactly one place- a singleton manager that allows any other classes (mostly static ones) to tie into those loops when they want to via events. This also allows for priority orders in cases where that would be useful- EarlyUpdate, Update, LateUpdate, and others that run on completely different TimeScales from the normal game (allowing slowing down time for some objects and not others, etc…).

Anyways, most of that is just my personal view on the subject- I’m sure there are others. If you’re looking for an EXACT timer though, coroutines may typically be slightly closer than Update, but they’ll both have the same problem of needing to return on normal Unity event cycles, and neither will ever be exact. If you need exact, you’ll likely have to resort to running on a new thread, and the return of data from that thread will still have to be during the normal Unity event cycles anyways. shrugs

Cheers!

Both do just fine in most cases, but if you want something to happen at exactly 1 second (not 0.9999999 neither 1.0000001), use the Coroutine.

I mostly use Update because i sometimes have i mis-use Coroutines sometimes and get bugs. Plus, i can control things on my Update on my own way. The plain-old:

if(x<maxTime){
x+=time.deltaTime;
}else{
CallFunction();
}

Works just fine :slight_smile: