- Home /
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
Answer by x4637x · Jan 04, 2018 at 04:45 AM
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.
They should actually have the same problem, just to a slightly lesser degree. They run on the same thread, so coroutines return from yields on one of the normal Unity event cycles (Update, LateUpdate, OnGUI, etc...). Since it's checked more than once per frame though, and not fixed to JUST the Update loop, it should usually be closer to the "perfect return time".
Just ran a test on this, you are right. They both have this problem and coroutines does return a closer result. Getting 1.02 second from coroutines and 1.04 second from update call.
To some people, maybe not much a difference. But if you are using this in a fast-paced game like a rhythm game or so, it is a noticeable difference.
Don't take my test data for granted. I am not tested this in an empty project, which means there are some other factors is affecting this result. You might want to test this on your own project.
Answer by Lysander · Jan 04, 2018 at 04:53 AM
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!
Answer by black_sheep · Jan 04, 2018 at 05:28 AM
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 :)
Coroutines still return on the same Unity event loops, so it'll almost never be perfect either, but because it isn't limited to only the Update loop specifically (it can return on LateUpdate, or FixedUpdate, or OnGUI if they occur first after the target time has been reached), it's usually a bit closer. See my answer, or the comments on x4637x's answer.
Your answer
Follow this Question
Related Questions
Problem with timer in Update function 1 Answer
Each update runs my code 6 times per line. Is it a bug? 1 Answer
How to destroy in an amount of time 1 Answer
Changing 2 objects position over time 1 Answer
How IEnumerator Update() works? 2 Answers