Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
16
Question by David C · Jun 06, 2011 at 04:57 PM · coroutines

Can i check if a coroutine is running?

hello, i have multiple coroutines and i want certain ones to run in certain conditions so my simple question is can i have an if statement were i check if a coroutine is running?

so kind of like:

 if( coroutineX is running)
 {
     //do stuff
 }

thanks!

Comment
Add comment · Show 1
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 David C · Jun 06, 2011 at 07:05 PM 0
Share

is this even possible?

8 Replies

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

Answer by Headworker · Sep 28, 2013 at 07:37 PM

The answer may be quite simple:

Just use a bool like this: bool CR_running;

 void InvokeMyCoroutine()
 {
      StartCoroutine("Coroutine");
 }
 
 IEnumerator Coroutine()
 {
    CR_running = true;
    //do Stuff
    yield return //Whatever you want
    CR_running = false;
 }
 
 // As long as you a within this scope you can just do :
 
 void AnotherFunction()
 {
    if (CR_running)
    {
        // Do some other stuff;
    }
 
 }
Comment
Add comment · Show 11 · 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 letoast · Jan 06, 2014 at 08:25 AM 1
Share

I'm not completely sure, but I think your method will not set the CR_running flag to false, because it will already yield before it. I think that you have to set CR_running to false before you yield return.

avatar image NikoBusiness · Jan 06, 2014 at 08:30 AM 0
Share

@letoast you are right it wont work

avatar image Headworker · Jan 18, 2014 at 10:54 PM 1
Share

If you use it with a

 yield WaitforSeconds / WaitForEndofFrame

or any other wait function, it does work, Ive been using it for quite a while now.

avatar image isteffy · Jun 01, 2016 at 11:08 AM 1
Share

This worked for me and is the best answer imo

although I used

  if (crRunning == false) {
      StartCoroutine ("StartCorRechargeEnergy");
  }

ins$$anonymous$$d of

  if (crRunning) {
      StartCoroutine ("StartCorRechargeEnergy");
  }
avatar image jj_unity328 · Aug 10, 2018 at 02:23 PM 3
Share

It won't handle StopCoroutine will it?

avatar image JanZagar jj_unity328 · Dec 06, 2020 at 03:22 PM 0
Share

I think it won't. So you just have to manually set the bool to false after StopCoroutine

Show more comments
avatar image
6

Answer by $$anonymous$$ · May 20, 2013 at 09:10 AM

I don't know if this is they way they have done it in the program Barrett Fox talks about, but one way I have used is this:

You can hijack the IEnumerator like this:

 IEnumerator someCoroutine = aCoroutine();
 
 Coroutine startSomeCoroutine = StartCoroutine(someCoroutine );
 
 while (someCoroutine.MoveNext())
 {
    Debug.Log("Im running");
    yield return null;
 }
 yield return startSomeCoroutine;
 

So when the loop is over, the routine is done.

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 B00TALMIGHTY · Aug 06, 2013 at 03:37 PM 2
Share

Problem with this is you are running the coroutine twice as fast, which may or may not be an issue. The coroutine will recieve a movenext from you and from unity.

avatar image $$anonymous$$ · Jan 06, 2014 at 08:33 AM 0
Share

I see, that was not a problem in my solution though. But obviously that is not the best solution then. Is there a way to use my idea without doing two $$anonymous$$oveNext's?

avatar image
3

Answer by Epholl · Apr 02, 2015 at 07:39 AM

I had a similar problem and came with a solution based on Headworker's.

 public IEnumerator Coroutine() {
     coroutineRunning = true;
     while (courutineCondition) {
         DoStuff();
         yield return null;
     }
     coroutineRunning = false;
 }



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 Firedan1176 · Dec 26, 2015 at 08:51 PM 0
Share

Wouldn't this be the simplest and the easiest implementation? What could be wrong with this?

avatar image ab-dh · Mar 30 at 08:24 AM 0
Share

If you hit an err, coroutineRunning is forever false.

avatar image
3

Answer by GingerLoaf · Jun 03, 2015 at 10:23 PM

Why not create a quick framework to handle this? After reading all the comments on this page, I had several ideas of my own that all begin with my own custom StartCoroutine method.

PLEASE NOTE: This is experimental code only and is not optimized at all. This is only to serve the purpose of a prototyped idea.

I would do something similar to this:

public class TrackedCoroutineBehaviour : MonoBehaviour {

 private List<string> runningCoroutinesByStringName = new List<string>();
 private List<IEnumerator> runningCoroutinesByEnumerator = new List<IEnumerator>();

 public Coroutine StartTrackedCoroutine(string methodName)
 {
     return StartCoroutine(GenericRoutine(methodName, null));
 }

 public Coroutine StartTrackedCoroutine(IEnumerator coroutine)
 {
     return StartCoroutine(GenericRoutine(coroutine));
 }

 public Coroutine StartTrackedCoroutine(string methodName, object parameter)
 {
     return StartCoroutine(GenericRoutine(methodName, parameter));
 }

 public bool IsTrackedCoroutineRunning(string methodName)
 {
     return runningCoroutinesByStringName.Contains(methodName);
 }

 public bool IsTrackedCoroutineRunning(IEnumerator coroutine)
 {
     return runningCoroutinesByEnumerator.Contains(coroutine);
 }

 public void StopTrackedCoroutine(string methodName)
 {
     if (!runningCoroutinesByStringName.Contains(methodName))
     {
         return;
     }

     StopCoroutine(methodName);
     runningCoroutinesByStringName.Remove(methodName);
 }

 public void StopTrackedCoroutine(IEnumerator coroutine)
 {
     if (!runningCoroutinesByEnumerator.Contains(coroutine))
     {
         return;
     }

     StopCoroutine(coroutine);
     runningCoroutinesByEnumerator.Remove(coroutine);
 }

 private IEnumerator GenericRoutine(string methodName, object parameter)
 {
     runningCoroutinesByStringName.Add(methodName);

     if (parameter == null)
     {
         yield return StartCoroutine(methodName);
     }
     else
     {
         yield return StartCoroutine(methodName, parameter);
     }

     runningCoroutinesByStringName.Remove(methodName);
 }

 private IEnumerator GenericRoutine(IEnumerator coroutine)
 {
     runningCoroutinesByEnumerator.Add(coroutine);
     yield return StartCoroutine(coroutine);
     runningCoroutinesByEnumerator.Remove(coroutine);
 }

}

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 Riiich · Jul 06, 2020 at 12:45 PM 1
Share

"A quick framework" xD

avatar image
1

Answer by Barrett-Fox · Jun 06, 2011 at 07:12 PM

This script on the Assest Store ($25) has an "isRunning()" method which is what you're asking for. But I would be interested to hear an answer about how to do this directly.

http://forum.unity3d.com/threads/92149-Coroutine-Manager-on-the-Asset-Store-Start-Pause-Resume-Reset-your-coroutines?highlight=coroutine

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 David C · Jun 06, 2011 at 07:43 PM 0
Share

this does seem better but i would like to try "IsInvoking" before i spend moeny and i am not at the right computer to test it right now but thanks for all the help guys

avatar image almo · Jun 06, 2011 at 07:49 PM 0
Share

I'm pretty sure my answer was wrong, looking back on it. It's possible you could have your coroutines set a static flag somewhere when they start, and clear them when they're done. But I think using this Asset Store thing is probably going to be better in the long run.

avatar image Talavang · Oct 22, 2012 at 01:33 AM 0
Share

is not the same. "invoke" set a timer and return nothing. "startcoroutine" return a Ienumerator.

avatar image isteffy · Jun 01, 2016 at 10:37 AM 4
Share

I wouldn't recommend this answer because it's asking someone to spend $25 when there are free answers below

avatar image brbackingtracks · Dec 17, 2018 at 01:51 AM 0
Share

Don't buy that, see @Headworker answer ins$$anonymous$$d!

  • 1
  • 2
  • ›

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

22 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

NullReferenceException in StartCoroutine 1 Answer

Game architecture problem 0 Answers

problems with coroutines 0 Answers

How to import the object from server to unity 2 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