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
7
Question by runonthespot · May 20, 2011 at 08:35 AM · updatecoroutinesfixedupdatelateupdate

Coroutines vs Update/FixedUpdate/LateUpdate

A friend on twitter, [@pixelplacement](www.twitter.com/pixelplacement) recently pointed out that he never uses Update anymore, instead preferring co-routines for everything, citing that it makes state management a dream (among other advantages).

Firstly, could someone explain how to replace "Update" with an equivalent coroutine, and variously how you sequence things under this paradigm, and secondly, are there any thoughts (Unity engine developers especially) as to whether this is a good way to go?

Comment
Add comment · Show 1
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 Owen-Reynolds · May 20, 2011 at 06:26 PM 0
Share

Think of something specific which shouldn't run 60 times/second, like AI, enemy spawning, flamethrower damage ticks... and mess around with converting it to a coroutine.

2 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by Eric5h5 · May 20, 2011 at 04:22 PM

If you want to literally replace Update with a coroutine, then do this:

 function Start () {
     while (true) {
         // do stuff here that you'd normally do in Update
         yield;
     }
 }

If that's all you're doing, then it has no advantage over Update. I wouldn't say never use Update; it's good for things which just do the same thing every frame. Coroutines are good for when you want to schedule events. For example, if you have an object moving forward, but you want it to stop for a second when you hit a key, you would do something like this if you use Update:

 private var wait = false;
 private var timer : float;
 
 function Update () {
     if (!wait) {
         transform.Translate(Vector3.forward * Time.deltaTime);
     }
     else if (Time.time >= timer) {
         wait = false;
     }
     if (Input.anyKeyDown) {
         wait = true;
         timer = Time.time + 1.0;
     }
 }

The thing is, that code isn't doing the same thing every frame. Sometimes it's doing one thing, sometimes another. So let's rewrite that as a coroutine:

 function Start () {
     while (true) {
         transform.Translate(Vector3.forward * Time.deltaTime);
         if (Input.anyKeyDown) {
             yield WaitForSeconds(1.0);
         }
         yield;
     }
 }

As you can see, there are no more global variables and slightly convoluted if/then logic. The code is shorter and basically self-documenting: "If you press any key, wait for a second." And this is just a very simple example...as states get more complex, coroutines get more powerful. See here for a somewhat more complex example.

Comment
Add comment · Show 4 · 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 Cripple · Sep 07, 2012 at 08:57 AM 0
Share

Sorry but your update code is not that good, you use too much things to achieve what you want.

 private float timer = 0;

 void Update() {
     timer -= Time.deltaTime;

     if (timer <= 0.0f) {
         transform.Translate(Vector3.forward * Time.deltaTime);
     }

     if (Input.any$$anonymous$$eyDown) {
         timer = 1.0f;
     }
 }
avatar image runonthespot · Sep 07, 2012 at 09:06 AM 0
Share

@Cripple I think the "self documenting" aspect of Eric5h5's coroutine still beats it though. Subsequent to this question I've found I've used coroutines a lot more, but I still favour Update in a lot of situations as I find coroutines can do many strange and unexpected things that are hard to debug. An Update at least does what it says on the tin.

avatar image sonhgc00016 · May 16, 2014 at 05:44 AM 0
Share

Can you do it again by C#. pls!

avatar image binary_biscuit · May 23, 2014 at 04:05 PM 0
Share

Am I right in thinking that until a coroutine hits a: yield break;

Then it will continue indefinitely, hence why you can use it as a substitute for an Update function?

avatar image
2

Answer by demize2010 · May 20, 2011 at 08:44 AM

Co-routines are fantastic for performance. Sometimes you have to perform actions every frames, often you don't. Co-routines are functions you can call every few seconds to do stuff like AI. I've never really considered it for state management but I'm pretty new to coding and tend to use booleans for that ;)

Here is the overview for co-routines:

http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

and the script ref:

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.StartCoroutine.html

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 runonthespot · May 20, 2011 at 08:49 AM 0
Share

Thanks for the answer @demize2010 I am vaguely familiar with the concept of coroutines, but am confused mostly as to how you'd get away with completely leaving Update() behind, without basically reinventing it? Isn't Update() effectively a coroutine?

avatar image demize2010 · May 20, 2011 at 09:07 AM 0
Share

It totally depends on your game and the script your writing. In theory you could just kick off a whole load of co-routines from start. You will still need to use update for something though I suspect.

To be honest In the game we are working on at the moment I think there are less then 20 lines of code which sit in update in the entire project - most stuff is functions called by events or co-routines ;)

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

6 People are following this question.

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

Related Questions

Should terrain detail data be set on update, fixedupdate, lateupdate or coroutines? 0 Answers

how to achieve a LateFixedUpdate effect 5 Answers

What's the execution order of Updates of different objects? 1 Answer

Climb animation in Update or FixedUpdate or LateUpdate?, 1 Answer

Rigidbody.AddForceAtPosition & functions 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