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 /
  • Help Room /
This question was closed May 30, 2016 at 10:58 PM by jtink for the following reason:

http://answers.unity3d.com/questions/1182632/problem-with-stopping-nested-coroutines-control-ne.html

avatar image
0
Question by jtink · May 30, 2016 at 10:07 PM · coroutine errors

Errors using StopCoroutine with nested coroutines

I am having trouble stopping nested coroutines using StopCoroutine. I included minimal code for a component (CoroutineTest) that reproduces the issue at the bottom of the question.

If I Play a scene in Unity with a CoroutineTest component attached and active to a game object, and then Stop the game a second later, I get the following errors while the game is trying to quit:

 coroutine->m_WaitingFor->m_ContinueWhenFinished == coroutine
 coroutine->m_RefCount > 0
 coroutine->m_CoroutineEnumeratorGCHandle != 0

All with the same callstack:

 UnityEngine.MonoBehaviour:StopCoroutine(Coroutine)
 CoroutineTest:OnDisable() (at Assets/CoroutineTest.cs:15)

The errors are a little vague but it looks like they some kind of garbage collection failure with my coroutines. I'm not sure what I'm doing wrong.

I noticed two things:

  1. I get no errors if I use StopAllCoroutines instead of StopCoroutine(outerRoutine)

  2. I get no errors if I no longer nest a call to StartCoroutine(InnerRoutine())

I would like to keep some coroutines running when the component is disabled, while stopping others, so StopAllCoroutines is not suitable for me. Is there a way I can use StopCoroutine without causing errors when the game stops?

Here is the code for the component:

 using System.Collections;
 using UnityEngine;

 public class CoroutineTest : MonoBehaviour
 {
     private Coroutine outerRoutine;

     public void OnEnable()
     {
         outerRoutine = StartCoroutine(OuterRoutine());
     }

     public void OnDisable()
     {
         StopCoroutine(outerRoutine);
     }

     private IEnumerator OuterRoutine()
     {
         yield return StartCoroutine(InnerRoutine());
     }

     private IEnumerator InnerRoutine()
     {
         yield return new WaitForSeconds(4f);
     }
 }
Comment
Add comment · Show 2
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 jtink · May 30, 2016 at 07:48 AM 0
Share

I just noticed that if I start and stop coroutines using strings ins$$anonymous$$d of IEnumerators (ex. StartCoroutine("OuterRoutine")), the code works without errors. So what's the difference with using Coroutine types as arguments?

avatar image jtink · May 30, 2016 at 07:57 AM 0
Share

I may have found my mistake! Re-reading the documentation for StopCoroutine I see that the signature is:

 public function StopCoroutine(routine: IEnumerator): void;

but I'm passing in a Coroutine ins$$anonymous$$d of an IEnumerator! This didn't cause any compile errors but I think it's a misuse of StopCoroutine nonetheless. The following code fixes the issue and I will do this from now on:

 IEnumerator outerRoutine;

 public void OnEnable()
 {
     outerRoutine = OuterRoutine();
     StartCoroutine(outerRoutine);
 }

 public void OnDisable()
 {
     StopCoroutine(outerRoutine);
 }


1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · May 30, 2016 at 10:43 PM

See this question and my answer

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

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException on StartCoroutine. Does anyone know what it could cause that? 0 Answers

firebase analytics freeze inside coroutine 0 Answers

Add points every five seconds 1 Answer

Array reference being changed without me knowing, or, When a coroutine is run on a monobehavior and then stopped with StopCoroutine (coroutine) it stops the same coroutine being run on another instance of the same monobehavior on a different object. 1 Answer

Coroutine Not working properly. 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