Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ina · Jan 04, 2018 at 04:34 AM · updatetimerienumerator

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lysander · Jan 04, 2018 at 05:01 AM 0
Share

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".

avatar image x4637x Lysander · Jan 04, 2018 at 05:18 AM 0
Share

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.

avatar image ina · Jan 04, 2018 at 06:09 AM 0
Share

is 1.02 vs 1.04 seconds noticeable to the human eye?

avatar image x4637x ina · Jan 04, 2018 at 06:14 AM 0
Share

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.

avatar image x4637x ina · Jan 04, 2018 at 06:18 AM 0
Share

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.

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lysander · Jan 04, 2018 at 05:32 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges