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 raphu604 · Apr 28, 2014 at 01:15 PM · yieldcoroutinesstopbreakend

Does a coroutine end itself automatically?

Hello guys, I have a simple question: does a coroutine terminate itself once it reaches the end of its code block?

I know I know... this is probably a very stupid question, but I just wanna make sure Unity doesn't secretly stack them somehow by using StartCoroutine() and keep them somewhere even though they aren't doing anything anymore.

I know that if I put an infinite while loop in a coroutine, the coroutine will never stop until you tell it to do so (via StopCoroutine(), StopAllCoroutines() or a boolean set in the while loop).

But even then, would it really end? Take a look at this simple example:

 IEnumerator TestCoroutine()
     {
         yield return new WaitForSeconds(0.75f);
         DoSomethingHere();
         yield return new WaitForSeconds(3f);
         DoSomethingElseHere();
         yield break; //Is this even needed?
     }

Would this coroutine end itself once it reaches the last line, since it is not in an infinite while or for loop? Because if yes, calling it several times (like 4 or 5 times every here and there) wouldn't affect performance overhead at all... right?

I'm just not sure about the usage of these coroutines, maybe it's just me but I have the weird feeling that if I start several of these coroutines over a timespan of many hours of gameplay they never terminate themselves (even though they "should") and get stacked somewhere where they shouldn't, and therefore make the performance suffer a lot.

Thanks in advance for your help,

-Raphael

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
20
Best Answer

Answer by Andres-Fernandez · Apr 28, 2014 at 01:50 PM

Yes, they do finish when their block of code ends (or when they find a return or a yield break), no need to worry. They are just like regular functions that are executed at the end of the frame (after the updates) and have the ability of stopping their execution by the use of the yield instruction. Besides that, pretty much like regular functions.

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 raphu604 · Apr 28, 2014 at 09:10 PM 1
Share

Thanks a lot for the clear and crisp answer! I really appreciate it :)

avatar image roconn · Aug 26, 2019 at 06:55 PM 0
Share

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

avatar image
8

Answer by HarshadK · Apr 28, 2014 at 01:55 PM

Yes, Co-routine ends itself automatically after it is completed. In the end they are just the functions.

For additional information, the memory management related to the Co-routines this article C# Memory Management for Unity Developers states that:

If you launch a coroutine via StartCoroutine(), you implicitly allocate both an instance of Unity's Coroutine class (21 Bytes on my system) and an Enumerator (16 Bytes). Importantly, no allocation occurs when the coroutine yield's or resumes, so all you have to do to avoid a memory leak is to limit calls to StartCoroutine() while the game is running.

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 raphu604 · Apr 28, 2014 at 09:12 PM 0
Share

Thanks for the clarification guys :)

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

23 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

Related Questions

What will StopCoroutine exactly do? 2 Answers

Trouble Resuming after Yielding while Inside Coroutine 1 Answer

Why is my "yield" statement stopping my script? 1 Answer

How do I use Coroutines remotely... and correctly ? 2 Answers

Having trouble using coroutines, making unity hang 0 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