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 /
  • Help Room /
avatar image
4
Question by dakshesh1010 · Mar 16, 2017 at 07:50 AM · optimizationcoroutinesupdate functionyield waitforseconds

Which is more efficient, Update() or a couroutine with while(true)?

So, I'm just a noob developer and I've been working on a project which was already in development before I joined. And instead of using Update(), coroutines are used in many places.

So I was just wondering which is more efficient. An Update() or a coroutine which looks tlike this:

 IEnumerator MyUpdate()
 {
      while(true)
      {
             yield return new WaitForSeconds(Time.deltaTime);
             //Update things here.
      }
 }


Comment
Add comment · Show 5
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 ExtinctSpecie · Mar 16, 2017 at 08:42 AM 0
Share

a co-routine like that doesnt execute every frame like Update() does the below would be the same as Update() method

 IEnumerator myUpdate()
     {
         while (true) 
         {
             yield return new WaitForEndOfFrame();
             print ("hello");
         }
     }

but still its just extra code i think Update function would be more efficient although im a newbie as well in unity and c#

avatar image ShadyProductions ExtinctSpecie · Mar 16, 2017 at 01:41 PM 0
Share

$$anonymous$$ight put this as answer, since it's a very solid answer.

avatar image Bunny83 ExtinctSpecie · Mar 16, 2017 at 02:12 PM 1
Share

That's actually wrong. This coroutine would not behave the same as Update. It would be the same when you yield return null; ins$$anonymous$$d. WaitForEndOfFrame let you execute code at the very end of the frame, even after rendering has completed. Update runs more or less "in the middle" of the frame.

avatar image Kyle_WG Bunny83 · Mar 16, 2017 at 02:15 PM 0
Share

^^ I second this. from @Bunny83

Highly useful to look into: https://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by Kyle_WG · Mar 16, 2017 at 01:56 PM

Coroutines are best when you have a specific condition to wait for. As in; waiting for a threaded job to finish or in this case waiting for a certain amount of time. Coroutines also generate garbage, especially when you're yielding with yield return new WaitForendofFrame() As every time you yield that object is created.

Unity, internally, holds a list off all things needing to update essentially and traverses that when everything needs to update. Definitely more advantagous as there's features to explicitally state execution order.

I recommend this entirely over coroutines.

Comment
Add comment · Show 2 · 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 Kyle_WG · Mar 16, 2017 at 02:08 PM 0
Share

I highly recommend learning this Script Lifecycle Flowchart. It will show when your code is executed within the engine and what the different yield types are. It'll help you keep game, physics and rendering logic in their right places.

https://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html

avatar image Bunny83 · Mar 16, 2017 at 02:08 PM 5
Share

It is true that WaitForEndOfFrame and WaitForSeconds are objects which generate garbage each time you create a new instance. However those can be simply cached and reused in which case it doesn't behave much different from Update. There might be some additional overhead involved but it's generally not that much to even think about.

Coroutines, as you said, let you wait for specific "events" which Update can't do at all. For example WaitForEndOfFrame. This let you execute code after the rendering of the current frame has completed this can't be done with Update. It can be done with some of the Camera callbacks, but for this the script need to be attached to the camera.

Where coroutines are nice as well is when you need to perform a task every frame for a limited time period. When using Update you either have to use a boolean flag or any other mechanic that prevents the code from executing when not needed, or you have to disable the whole script.

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

70 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

Related Questions

Using Coroutines and Update together 0 Answers

How to properly call Async Task 0 Answers

loop inside update 0 Answers

Unity C# Punching Coroutine not completing 0 Answers

Why Unity docs do not use New with WaitForSeconds and yield? 1 Answer


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