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
0
Question by sandworm · Aug 01, 2012 at 11:29 PM · performancecoroutineyieldstartcoroutinecpu

Fire and forget coroutines

I have a number of performance-heavy methods that don't need to finish at the same time as the main method. I've been trying to use them as coroutines such as the following:

 IEnumerator Example() {
      yield return null;
      Do stuff ...
 }


Then I simply call them with StartCoroutine(), and it works great in the editor, reducing spikes and so-on. However, on the device it appears to never resume the coroutine. Do I really need to yield return StartCoroutine(Example()); ? Or is it possible to fire-and-forget them and trust that they will execute to the end?

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
1

Answer by sandworm · Aug 02, 2012 at 02:37 AM

Ok, solved my own problem. Turns out the object that calls the StartCoroutine cannot be destroyed in the next frame, or the called coroutine will never get past the first yield statement. I guess the coroutine is tied to the caller. Once I switched the caller to a persistent GO, it worked perfectly.

Comment
Add comment · Show 5 · 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 Kryptos · Aug 02, 2012 at 01:31 PM 0
Share

Yes the coroutine executor is the object that called the StartCoroutine().

Note that if you call a coroutine on another object, neither the caller or this other object should be destroyed, meaning you can't fire and forget this way.

A workaround is to have the caller call a simple method that will in turn start the coroutine. Something like (not tested):

 public class Caller : $$anonymous$$onoBehaviour
 {
     public ScriptWithCoroutine swc;

     void Some$$anonymous$$ethod()
     {
         swc.SomeAction();
     }
 }

 public class ScriptWithCoroutine : $$anonymous$$onoBehaviour
 {
     public void SomeAction()
     {
         StartCoroutine(DoSomeAction());
     }

     IEnumerator DoSomeAction()
     {
         bool finished = false;
         while (!finished)
         {
             // do some expensive and iterative work
             yield return null;
         }
     }
 }
avatar image sandworm · Aug 02, 2012 at 03:37 PM 0
Share

This is awesome info, thanks. It's something I never realized about coroutines, why isn't it in the docs?

avatar image Bunny83 · Aug 02, 2012 at 04:16 PM 0
Share

@$$anonymous$$ryptos: That's not entirely true ;) In your case the caller can be destroyed without any problems since your coroutine runs on the ScriptWithCoroutine instance since you called StartCoroutine on this instance.

 StartCoroutine(DoSomeAction());
 // is the same as
 this.StartCoroutine(DoSomeAction());

So the coroutine runs on the swc $$anonymous$$onoBehaviour. The Caller class isn't involved except it is calling StartCoroutine.

But, maybe that's what you've ment, when you use StartCoroutine of another $$anonymous$$onoBehaviour, the object that contains the coroutine should not be destroyed and the object the coroutine runs on should not be destroyed:

 public class Caller : $$anonymous$$onoBehaviour
 {
     public ScriptWithCoroutine swc;
     void Start()
     {
         StartCoroutine(swc.SomeAction());
     }
 }
 
 public class ScriptWithCoroutine : $$anonymous$$onoBehaviour
 {
     public IEnumerator DoSomeAction()
     {
         bool finished = false;
         while (!finished)
         {
             // do some expensive and iterative work
             yield return null;
         }
     }
 }

In this case the coroutine belongs to the swc object, but it runs on the Caller instance.

avatar image Kryptos · Aug 03, 2012 at 08:28 AM 0
Share

@Bunny83 that is exaclty what I meant. The piece of code I gave was a solution to prevent this issue (hence the word workaround in my comment). ;)

avatar image Bunny83 · Aug 03, 2012 at 11:19 AM 0
Share

Yes, sorry, i get that wrong ;)

Another workaround is to call StartCoroutine of the desired owner:

 public class Caller : $$anonymous$$onoBehaviour
 {
     public ScriptWithCoroutine swc;
     void Start()
     {
         swc.StartCoroutine(swc.SomeAction());
     }
 }
 
 public class ScriptWithCoroutine : $$anonymous$$onoBehaviour
 {
     public IEnumerator DoSomeAction()
     {
         bool finished = false;
         while (!finished)
         {
             // do some expensive and iterative work
             yield return null;
         }
     }
 }

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

8 People are following this question.

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

Related Questions

How To Create Coroutine Delegates? 1 Answer

WaitForSeconds problem with Unity Pro 3 Answers

Yield position within while loop in coroutine 1 Answer

JS Wait for a function to complete - no WaitForSeconds 1 Answer

Coroutines and states 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