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 …
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
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:
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.