Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Henri 1 · Mar 23, 2011 at 06:06 AM · animationcoroutineinvalidcastexceptionstopping-animation

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()); }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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");
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Henri 1 · Mar 23, 2011 at 11:31 AM 0
Share

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

avatar image Mike 3 · Mar 23, 2011 at 12:02 PM 0
Share

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?

avatar image Henri 1 · Mar 23, 2011 at 03:25 PM 0
Share

It worked! I should not have added it as a variable! Cool stuff man! Highly appreciated!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges