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
7
Question by Mike 12 · Dec 04, 2010 at 08:49 PM · coroutinecollections

Mechanics of Coroutines

How does Unity execute the coroutines? I take it that the execution somewhat resembles threading, but I am unclear on what's happening when a coroutine is executed.

Are the coroutines a collection of delegates which, when the IEnumerator gets to that particular delegate, will execute a function?

What happens when the coroutine calls yield? Like, for instance, when it calls "yields return WaitForSeconds()" does the iterator go to the next coroutine or does it wait for that coroutine to finish before going to the next? And, when the coroutine calls "yield break", does it remove that coroutine from the collection of 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

1 Reply

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

Answer by _Petroz · Dec 04, 2010 at 09:35 PM

The behavior of Coroutines is more like time slicing than threading as all coroutines are executed from the main thread.

https://docs.unity3d.com/Manual/ExecutionOrder.html

  • yield; The coroutine will continue after all Update functions have been called on the next frame.
  • yield WaitForSeconds(2); Continue after a specified time delay, after all Update functions have been called for the frame
  • yield WaitForFixedUpdate(); Continue after all FixedUpdate has been called on all scripts
  • yield WWW Continue after a WWW download has completed.
  • yield StartCoroutine(MyFunc); Chains the coroutine, and will wait for the MyFunc coroutine to complete first.

Coroutines are reentered at the point where they returned.

I think that is all the public information about Coroutines. It's always safest to avoid making assumptions based on the underlying implementation since it is not public and could therefore change at any time.

More info:

https://docs.unity3d.com/Manual/Coroutines.html

https://docs.unity3d.com/ScriptReference/Coroutine.html

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 yoyo · Feb 04, 2011 at 08:45 PM 1
Share

yield is a C# keyword, documented here -- http://msdn.microsoft.com/en-us/library/9k7k7cf0(VS.80).aspx. Basically the IEnumerator / yield return construct used for coroutines means the Unity engine can iterate over the multiple results returned by the coroutine.

avatar image Akeronz · Aug 25, 2013 at 01:37 AM 0
Share

Thanks @_Petroz !! I did not know you could chain coroutines and this is exactly what I needed!

avatar image Romeno · Apr 05, 2019 at 09:30 PM 0
Share

first 2 links are broken by now. First link is now https://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html

avatar image Bunny83 Romeno · Apr 06, 2019 at 12:17 AM 1
Share

I've replaced the links with working links.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Collection Change During Iteration 0 Answers

Bullet Script 2 Answers

yield return null vs yield return WaitForEndOfFrame 4 Answers

Can you check a boolean in a coroutine? 2 Answers

application loadlevel c# calling coroutine not working? 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