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 /
avatar image
0
Question by Pivetta · Jul 10, 2019 at 05:22 PM · coroutineorder-of-execution

Coroutine with reference not stopping - Missed the out keyword

Using Unity 2019.1.9f1

I just fell into this problem with a simple Coroutine manager I wrote, the problem was strangling me so i decided to write an example to be sure the problem was not my fault. Or else I made a wrong assumption on how the Coroutine system is meant to work.


The problem is:

  • I start a coroutine and save it reference

  • I stop the coroutine and start a new one using the same reference by changing it's target IEnumerator

  • !!! The coroutine actually won't stop, the new one starts (on the same reference?)


Here is the code, it's simple, a phone is ringing, the user by pressing D (running on script 2) invokes an unity event, the telephone (SHOULD) stop ringing and the user should answer.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DEBUGCoroutines : MonoBehaviour
 {
     //DIFFERENT STATES
     public enum DebugState { Ringing, Pickup, Stop }
     public DebugState state = DebugState.Ringing;
 
     Coroutine stateRoutine = null;
 
     void Start()
     {
         //START EXAMPLE BY RINGING
         UpdateState(state, true);
     }
 
     //STATE UPDATER, ON CALL CHANGES STATE
     void UpdateState(DebugState s, bool force = false)
     {
         if (s != state || force)
         {
             state = s;
 
             CustomStopAllCoroutines();
             switch (state)
             {
                 case DebugState.Ringing:
                     CustomStartCoroutine(stateRoutine, Ring());
                     break;
 
                 case DebugState.Pickup:
                     CustomStartCoroutine(stateRoutine, Pickup());
 
                     break;
                 case DebugState.Stop:
                     CustomStartCoroutine(stateRoutine, Stop());
                     break;
 
                 default:
                     break;
             }
         }
 
     }
 
 
     //STATE EVENTS
     //
     IEnumerator Pickup()
     {
         Debug.Log("HELLO!?");
         yield return new WaitForSeconds(5f);
         Debug.Log("Ok, Good bye");
         UpdateState(DebugState.Stop);
     }
 
     IEnumerator Ring()
     {
         while (state == DebugState.Ringing)
         {
             Debug.Log("ring");
             yield return new WaitForSeconds(2f);
         }
         Debug.Log("ERROR"); //THIS SHOULDN'T PLAY!
     }
 
     IEnumerator Stop()
     {
         yield return new WaitForSeconds(10);
         UpdateState(DebugState.Ringing);
     }
 
     
     //METHOD CALLED BY THE EVENT
     public void GetCall()
     {
         UpdateState(DebugState.Pickup);
     }
 
 
     //COROUTINE MANAGERS
     //
     void CustomStopAllCoroutines()
     {
         QuitRoutine(stateRoutine);
     }
     void CustomStartCoroutine(Coroutine cr, IEnumerator routine)
     {
         if (cr == null) cr = StartCoroutine(routine);
         else
         {
             StopCoroutine(cr);
             cr = null;
             cr = StartCoroutine(routine);
         }
     }
 
     void QuitRoutine(Coroutine cr)
     {
         if (cr != null) StopCoroutine(cr);
         cr = null;
     }
 }

And here the simple event caller:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Events;
 
 public class DEBUGCaller : MonoBehaviour
 {
     public UnityEvent debugEvent;
 
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.D))  debugEvent.Invoke();
     }
 }


Comment
Add comment · Show 3
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 Pivetta · Jul 10, 2019 at 05:52 PM 0
Share

I am not using StopAllCoroutines() because (not in this simple example) I am stopping only isolated routines and have others that meanwhile must run in background.

avatar image Pivetta · Jul 10, 2019 at 06:22 PM 0
Share

Using StopAllCoroutines() indeed works, I also tried to change while (state == DebugState.Ringing) into a while(true) and the error is still here - so it has not to do with the Order of Execution I guess.

avatar image Pivetta · Jul 10, 2019 at 06:51 PM 0
Share

Other updates, I noticed I was mixing IEnumerators and Coroutine, I changed everything to IEnumerator (I am not allowed to pass as method variable an IEnumerator to a Coroutine). The behaviour of this script didn't change. I am still stuck behind the same issue.

1 Reply

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

Answer by Pivetta · Jul 10, 2019 at 07:03 PM

Found the solution and it was my mistake.


I was not really changing my Coroutine or IEnumerator variable (had to change to IEnumerator cause I could not define the Coroutine member from a Method Variable - I bet the ref keyword would do)


All I had to do was using the out keyword to save my IEnumerator outside the method, now everything is working smooth


The changed parts of the code:

 //stateRoutine is now an IEnumerator
 IEnumerator stateRoutine = null;
 
 //In the switch now I had to use out
 CustomStartCoroutine(out stateRoutine, Ring());
 
 //This is now the fixed method to start coroutines
 void CustomStartCoroutine(out IEnumerator cr, IEnumerator routine)
     {
         cr = routine;
         StartCoroutine(cr);
     }
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

116 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 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 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 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 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 avatar image

Related Questions

Execution order when Start is declared with IEnumerator return (as a Coroutine) 1 Answer

Is any coroutine called before LateUpdate? 1 Answer

Call function after other objects have run their Start() function 2 Answers

Issue With Simultaneous ReadPixels Calls 0 Answers

How to Substitute Coroutine in non-MonoBehaviour Class? 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