- Home /
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()); }
}
Answer by Mike 3 · Mar 23, 2011 at 07:50 AM
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");
Thank you very much... But I get the following error: "Assets/Standard Assets/Character Controllers/Sources/Scripts/Powerup2.js(8,10): BCE0089: Type 'Powerup2' already has a definition for 'COPowerUp'." Any Ideas? I'm new to scripting and do not know what to change :( I would REALLY appreciate your help dude... Thanks
I'm not exactly sure what you've added to give that error - could you paste the code exactly as is as an edit to your question?
It worked! I should not have added it as a variable! Cool stuff man! Highly appreciated!!
Your answer
Follow this Question
Related Questions
Animation Curve or Coroutine? Performance question. 1 Answer
Coroutine / Switch case 1 Answer
Best way to decouple character attack animations? 0 Answers
boolean to stop animation does not work? 1 Answer
Scale with coroutine and lerp 1 Answer