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
1
Question by Fattie · Sep 23, 2013 at 08:46 AM · monobehaviourcoroutines

Is there a way to tell if a particular coroutine is running?

Is there a way to tell if a particular coroutine is still running?

Imagine you have a class

 public IEnumerator ImagineThis() {}

and it contains functions that are coroutines, perhaps one called IEnumertaor Foo().

Please, do not answer that Foo could set a flag while it is running, and you could look at that. The question is, can you look at ImagineThis in some way and determine if Foo() is "running" right now.

One would imagine that at some level, MonoBehaviour - or something - must keep track of which coroutines it has to come back to in the next frame; if so, there's the answer ???

Any ideas?

Noting this question ...

http://answers.unity3d.com/questions/541431/how-the-heck-do-you-check-if-start-is-still-runnin.html

Answering this question would additionally answer that question.

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

4 Replies

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

Answer by robhuhn · Sep 23, 2013 at 02:44 PM

I would say there is no way to determine which routines are running - Especially in a case where the coroutine is started by unity like

 public IEnumerator Start()
 {
     yield return new WaitForSeconds(5f);
 }

All routines end up being passed to external methods which should then be handled by c++ in a non-reachable scope.

 [WrapperlessIcall ]
 [MethodImpl (MethodImplOptions.InternalCall)]
 public extern Coroutine StartCoroutine_Auto (IEnumerator routine);

 [WrapperlessIcall ]
 [MethodImpl (MethodImplOptions.InternalCall)]
 public extern Coroutine StartCoroutine (string methodName, object value);

Please correct me if I'm wrong.

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 Fattie · Sep 23, 2013 at 05:13 PM 1
Share

(You must be on the wrong site because you actually answered the question at hand :) )

I'm thinking ....... say, you can cancel a coroutine right? hell if invoke you can cancel it by name .. so maybe that suggests a way to know if it's still going?

avatar image Fattie · Sep 30, 2013 at 06:38 AM 1
Share

I guess you've pretty much answered the question @Rob - it's "no, you can't" eh!

That seems to be the size of it - it is interesting to me since, I mean obviously "unity know" one is running, since it can easily be cancelled etc, and they get stopped if the gameObject is active-false'd, so, clearly, "unity must know" what coroutines are running.

I actually don't understand the complex ob-c you posted, or what it is! :) But it looks like you know what you're talking about

thanks again!

avatar image
5

Answer by ArkaneX · Sep 23, 2013 at 11:02 PM

This is not exactly the info you're looking for, but please check this thread:

http://forum.unity3d.com/threads/202064-Extended-coroutines

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 Fattie · Sep 30, 2013 at 06:36 AM 0
Share

fascinating link

avatar image dbrizov · Jul 29, 2014 at 10:04 AM 0
Share

Very helpful extension. Just what I needed.

avatar image
1

Answer by dropsofwaterfromchina · Aug 04, 2018 at 01:51 PM

 public enum CoroutineState
 {
     Ready,
     Running,
     Finished
 }
 
 public class CoroutineController
 {
     private IEnumerator routine;
     private Coroutine coroutine;
     public CoroutineState state;
 
     public CoroutineController(IEnumerator routine)
     {
         this.routine = routine;
         state = CoroutineState.Ready;
     }
 
     public void Start()
     {
         if (state != CoroutineState.Ready)
         {
             throw new System.InvalidOperationException("Unable to start coroutine in state: " + state);
         }
 
         state = CoroutineState.Running;
         coroutine = CoroutineHelper.Instance.StartCoroutine(RealRun());
     }
 
     private IEnumerator RealRun()
     {
         yield return CoroutineHelper.Instance.StartCoroutine(routine);
         state = CoroutineState.Finished;
     }
 
     public void Stop()
     {
         if (state != CoroutineState.Running)
         {
             throw new System.InvalidOperationException("Unable to Stop coroutine in state: " + state);
         }
         CoroutineHelper.Instance.StopCoroutine(coroutine);
         state = CoroutineState.Finished;
     }
 }
 
 /// <summary>
 ///  地球人自取。。。
 /// </summary>
 public class CoroutineHelper : MonoBehaviour
 {
     private static CoroutineHelper ins;
     public static CoroutineHelper Instance
     {
         get
         {
             if (ins == null)
             {
                 var go = new GameObject("CoroutineHelper");
                 DontDestroyOnLoad(go);
                 ins = go.AddComponent<CoroutineHelper>();
             }
             return ins;
         }
     }
 
     public void StartCoroutineEx(IEnumerator routine, out CoroutineController coroutineController)
     {
         if (routine == null)
         {
             throw new System.ArgumentNullException("routine");
         }
         coroutineController = new CoroutineController(routine);
         coroutineController.Start();
     }
 }

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 gitlinjoss · Oct 07, 2021 at 04:33 PM 0
Share

yo thank you for this! I had a similar idea

avatar image
0

Answer by superventure · Oct 01, 2013 at 12:48 AM

You could always set up booleans that are only triggered when the coroutine is enabled, and ended when the coroutine is over.

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 Fattie · Oct 01, 2013 at 07:01 AM 0
Share

hi super! Don't forget, that's precisely what the question's about, can you "actually look" and see, other than just setting a flag. ie, note I said "Please, do not answer that Foo could set a flag while it is running, and you could look at that." - heh!! Cheers...

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

Yield return null? 3 Answers

Wrapping Monobehavior? 0 Answers

WaitForEndOfFrame vs Time.frameCount 1 Answer

Serializable class with coroutines? 1 Answer

Example of co-routine for documentation 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