CORoutine does not completely stop

Hi,

Got some code with the help of this site, but have a little problem.

When I press the applicable button, and hold it in, everything works, but if I only press the button and release it, it stops the first animation in the CORoutine (PowerUp), waits for the actual animation time to finish, but does not stop the second one (PowerUpLoop) so the PowerUpLoop keeps playing over and over.

I cannot use Animation.Stop(PowerUpLoop) with the StopCORoutine(COPowerUp()) if the button is released, otherwise I get a "InvalidCastException: Cannot cast from source type to destination type" error.

Any Ideas?

var PowerUp : String = "PowerUp"; var PowerUpLoop : String = "PowerUpLoop";

private var inTransition : boolean = false; private var isPowerUpLoop : boolean = false;

function COPowerUp() { inTransition = true; isPowerUpLoop = true; var state : AnimationState = animation[PowerUp];

state.wrapMode = WrapMode.Once;
state.speed = 1.0;
animation.Play(PowerUp);
yield new WaitForSeconds(state.length);
(animation.Play(PowerUpLoop));

inTransition = false; 

}

function Update () { var horizontal = Input.GetAxis("Horizontal"); var vertical = Input.GetAxis("Vertical");

if (Input.GetButtonDown("Power Up")) {
    StartCoroutine(COPowerUp());
    }

if (Input.GetButtonUp("Power Up")){
    animation.CrossFade("idle");
    inTransition = false;
    isPowerUpLoop = false;
    StopCoroutine(COPowerUp());
}

}

You can't use StopCoroutine(Coroutine()), it only takes a string, and only stops coroutines started with string

What you need to do is start with

StartCoroutine("COPowerUp");

and end it with

StopCoroutine("COPowerUp");