[resolved] StopCoroutine IEnumerator doesn't work?

Hello,

I have several powerUps in my game wich the player can take. When he takes one powerUp, it triggers an effect, the effect last 10 seconds and then the effect disappear.

I have tried this twos types of code :

       public void OnPickUp(string pw) {
          OnBeforeDelay (pw);  //set player jump height to a higher one
          thePlayer.ResetEffect(pw);
          StopCoroutine ("ProcessPowerUpJumpBoost");
          StartCoroutine ("ProcessPowerUpJumpBoost");

          or

          private IEnumerator COJumpBoost;

          OnBeforeDelay (pw);  //set player jump height to a higher one
          thePlayer.ResetEffect(pw);
          COJumpBoost = ProcessPowerUpJumpBoost();
          StopCoroutine (COJumpBoost);
          StartCoroutine (COJumpBoost);
            
   }

        public IEnumerator ProcessPowerUpJumpBoost() {
          yield return new WaitForSeconds (timeEffect);
          OnAfterDelay ("JumpBoost"); // set player jump height to a normal one
        }

but …
If I trigger a different kinds of powerUp (differents effects) , this code work fine because the effect is applied and get back to normal after 10 seconds.

My problem occurs when I have two powerUps of the same kind following each other (It takes less than 10 seconds to reach the other).

What’s happening now is that if the player takes the jumpboost powerUp and 6 seconds after he takes an other jumpboost powerUp, the second effect will last only 4 seconds and not 10 seconds as I would like.

So, what I had thought was to stop the coroutine to prevent it from getting back to normal and then start again. But it doesn’t work …

Can you guys help me ?

Thanks

1 Like

Maybe it’s not working because you are reassigning it before stopping it?

Try putting this in start instead

COJumpBoost = ProcessPowerUpJumpBoost();

Or you could just use a coroutine type

private Coroutine COJumpBoost;
...
if (COJumpBoost != null) { StopCoroutine(COJumpBoost); }
COJumpBoost = StartCoroutine(ProcessPowerUpJumpBoost());
2 Likes

Thanks @Hikiko66 for replying,

I’ve tried your second solution, but it didn’t work well, so i’ve put some print:

           Debug.Log (COJumpBoost == null);
            if (COJumpBoost != null) {
                print ("JumpBoost already active");
                StopCoroutine (COJumpBoost);
            } else {
                print ("JumpBoost first tour");
            }
            COJumpBoost = StartCoroutine (ProcessPowerUpJumpBoost ());
            Debug.Log (COJumpBoost == null);

The results was … disturbing

At the first powerUp :

True
JumpBoost first tour
False

The second powerUp

True
False

So I’ve just replace the :

private Coroutine COJumpBoost;

by :

private static Coroutine COJumpBoost;

With this, your solution works ! … but i don’t know if this is a good way program (with the static)…

Now, this problem solved, I have an other error :
Coroutine continue failure

I’ve read about this error and it seems to be an unity error, but I don’t know if this apply to my code

Is this on the player or on the pickup?

private Coroutine COJumpBoost;

It’s on the PickUp

Then it’s a different variable for every pickup and doesn’t store the last started coroutine

But the “static” doesn’t resolve that ? or is it a messy thing ?

Should resolve it, yes. Just saying, that’s why it doesn’t work without static.

Regarding the new error, maybe switch back to iEnumator and make that static

http://answers.unity3d.com/questions/989547/coroutine-continue-failure-when-using-stopcoroutin.html

It doesn’t work with the IEnumerator, the second powerUp doesn’t even trigger so … I’ve tried differents solutions with the IEnumerator but …I think I will let with the coroutine.
It seems that this coroutine bug is fixed unity 5.6

Thank you @Hikiko66 for helping :slight_smile:

Hey,

i may be a little late, but i just stumbeld upon the same problem. My understanding is that the name StopCoroutine is a little confusing, as it seems to merely “suspend” the coroutine. So when you start it again via StarCoroutine, it will resume execution. My solution looks as follows:

private IEnumerator c1;

void Start()
    {
        c1 = cr1();
    }

public void StartMyCoroutine()
    {
        StartCoroutine(c1);
    }

public void RestartMyCoroutine()
    {
        StopCoroutine(c1);
        c1 = cr1();
        StartCoroutine(c1);
    }

private IEnumerator cr1()
    {
        Debug.Log("cr1 started.");
        while (true)
        {
            yield return new WaitForSeconds(0.1f);
            Debug.Log("cr1 running");
        }
    }

I think one has to reassign the Coroutine to the IEnumerator holding the instance for a complete execution from the beginning. So if you have PowerUps of the same type, stop the current coroutine, make a new assignment to your IEnumerator for the Coroutine beeing able to start anew. I figured this out with my Debug.Log(“cr1 started.”); statement, which wasn’t executed unless i reassigned cr1 to c1.

Hope this still helps all running into this problem.

2 Likes

This answer helped me. Thank you.