Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by anwserman · Jul 30, 2012 at 11:57 AM · coroutineupdateyieldthreading

Confused about using Update vs. Coroutines

Researching this topic, I came to these two conclusions.

  1. Coroutines are great for timed events

  2. Updates() and Coroutines behave exactly the same performance-wise. As in, the less Updates called and the less Coroutines running at any given time frame, the better performance you'll have (not to generalize, but removing code from being regularly updated is more significant than how it's being called)

That being said, I'm slightly stuck on how yielding works. Is this how it works?

 Frame 1: Unity main thread runs FunctionA
 Frame 1: FunctionB calls coroutine FunctionB
 Frame 1: FunctionB yields
 Frame 1: Since FunctionB yields and not finishes completely, FunctionA yields to Unity main thread
 Frame 1: Unity main thread gets control, moves onward with rendering
 Frame 2: Unity main thread notices FunctionA coroutined Function B
 Frame 2: FunctionB resumes control at point it left off at, function finishes out normally
 Frame 2: FunctionA gets resumed at point where it coroutined FunctionB, finishes out normally
 Frame 2: Unity main thread gets control, moves onward with rendering
 Frame 3: Unity main thread runs FunctionA
 Frame 3: FunctionA finishes out normally
 Frame 3: Unity main thread gets control, moves onward with rendering    
 ...etc...

If this is how yield works, than I can easily understand how this would be useful. If not, I need assistance!

http://answers.unity3d.com/questions/56415/increasedecrease-value-with-time-problem.html

http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html

http://answers.unity3d.com/questions/119076/coroutines-vs-updatefixedupdatelateupdate.html

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

2 Replies

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

Answer by Paulius-Liekis · Jul 30, 2012 at 12:19 PM

Is function FunctionA a coroutine? If not, then this is not happening: "Frame 2: FunctionA gets resumed at point where it coroutined FunctionB, finishes out normally".

There several ways to call yield and StartCoroutine. I find explicit syntax in C# much more clear, because Javascript ^tries to help you^, i.e. if it finds any yield statements in your function then it converts it to coroutine...

  1. Calling "StartCoroutine(FunctionB())" from FunctionA (where FunctionA is a couroutine or not - doesn't matter) will start FunctionB and then continue executing FunctionA without waiting for FunctionB to complete

  2. Calling "yield return StartCoroutine(FunctionB())" from FunctionA (where FunctionA is a couroutine) will start FunctionB and FunctionA will continue only after FunctionB is complete

  3. Calling "yield return null" from FunctionB (where FunctionB is a couroutine) will return to main thread and then continue FunctionB in next frame

BTW: Coroutines do not give you performance, they are just convenient when you need to delay something and similar situations...

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 anwserman · Jul 30, 2012 at 12:45 PM 0
Share

Ok, thank you for the clarification. This makes more sense now.

avatar image
0

Answer by IndieForger · Feb 17, 2013 at 04:24 AM

Biggest difference is that update is executed every frame and coroutine is basically task which can be delayed. I think it depends where you want to use it.

If you lets say cleaning particle system after it has finished rendering particles you should use coroutine in most cases along with IsAlive method. However if you have many particle system you should use update method in external controller that will keep an eye on all particle systems.

I wrote about it here

I did some test and coroutine might give you performance but should be used wisely. Managing 1000 coroutines against 1000 updates is not the best idea but then even 1000 updates doesn't make sense if all the calculations could be done in 1.

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

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

7 People are following this question.

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

Related Questions

Alternative for semaphores in Unity? 2 Answers

Threading in Unity 6 Answers

Invoke repeating OR coroutine with WaitForSeconds? 1 Answer

Is there a yield WaitForSeconds type code for the update? 2 Answers

Im a bit confused on a simple script 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