Home > Net >  How to know if a coroutine is still running?
How to know if a coroutine is still running?

Time:10-12

I have a coroutine which takes in some variable running in the update function and I need the code to be something like this:

void Update(){
   if(/*coroutine is not running*/){
      StartCoroutine(coroutine(some variable));
   }
}

Is there a way to know if the coroutine is still running before I run it with some other variable. I know that there is a way of doing it where I put that coroutine into another coroutine and use yield return coroutine(some variable) and that should work. But in my case the variable that the coroutine takes in depends on an event that my script is subscribed to, so the above implementation won't work. So is a way to know if my coroutine is still running or not?

CodePudding user response:

bool isCouroutineRunning = false;
void Update()
{
    if(isCouroutineRunning == false)
    {
        StopCoroutine("MyCourutine");
        StartCoroutine("MyCourutine",variable);
    }
}

IEnumerator MyCourutine()
{
    isCouroutineRunning = true;
    yield return new WaitForSeconds(1.0f);
    isCouroutineRunning = false;
}
  • Related