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
0
Question by rednax20 · Jun 09, 2013 at 03:59 PM · animationjavascriptcoroutinefunctionyield

Coroutine a function within a loop?

hey I'm working on a project in which I have a function that plays a punch animation. after 0.4 seconds the the punch changes into a kick (sort of like a combo). to make this happen I used yield waitforseconds(0.4). for some reason the function doesn't seem to be working. but when I take the yield out the function works fine (exept that it skips the punch animation and goes directly to the kick.) Is it imposible to coroutine a function within a while loop?

here's my code, to give you an idea of what I am doing.

 var attackf = function(theclass : String){
     if(thenewscript.hit == false){
         attack = true;
         if(theclass == punch){
             sanimation.Play("punch");
             yield WaitForSeconds(0.4);
             sanimation.Play("kick");
             attack = false;
                 
         }
         
     }
 };

 while(true){
     if(Input.GetKeyDown("n")){
         attackf("n");
     }
     yield;
 }

this strangely has no response when I call it from within my while loop, (and yes it is not possible for me to make this an update function). n is equal to the punch variable.

that wasn't my whole while loop mind you.

but when i change my function to this...

 var attackf = function(theclass : String){
     if(thenewscript.hit == false){
         attack = true;
         if(theclass == punch){
             sanimation.Play("punch");
             sanimation.Play("kick");
             attack = false;
                 
         }
         
     }
 };

The animation changes, but not the way i need it too.

Any suggestions? all help is greatly appreiciated. Sorry if this question has an obvious problem that I overlooked, like that time I spelt my function On Tigger Enter.

Comment
Add comment · Show 5
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 rednax20 · Jun 09, 2013 at 04:49 PM 0
Share

P.S. I'm using Unity 2d toolkit, does that change anything

avatar image Immanuel-Scholz · Jun 09, 2013 at 05:12 PM 0
Share

$$anonymous$$h... probably not related to your problem, but..: Do you want your main loop to continue after spawning the animations? $$anonymous$$aybe you wanted to change the call from attackf() into yield attackf() to pause the loop until the attack finished? If not, you have to make sure that attackf is not constantly called each frame as long as the user presses 'n' doing havoc with your sanimation states..?

avatar image rednax20 · Jun 10, 2013 at 12:21 AM 0
Share

Yes, it is necessary. I plan to use this function to hold multiple attacks that will be run by different input, (which must be in there own if statements for other reasons).

The function doesn't reek havoc because, as i stated above, that was not my whole while loop. I didn't include it all because it was 30 lines long.

Within it as well are functions for for movement. Originally I had the punch script in an update function in an outside script, but when the run button, and the punch button were pressed, there was some serious animation issues, in which two animations played at once.

I just want to know if my current script can work with the coroutines or do I have to do something else.

avatar image rednax20 · Jun 10, 2013 at 01:22 PM 0
Share

Yes that works perfectly! sorry about my lack of knowledge of monobehaviors, I'm relatively new at this.

Thanks a lot. I only wish you had answered so that i could select it as the correct answer.

If a ad$$anonymous$$ or someone else sees this could you please close this question, I can't do it myself

Thanks a lot everyone!

avatar image SolidSnake · Jun 10, 2013 at 01:26 PM 0
Share

ok i converted my comment to an answer not to confuse people :-)

No worries we are all learning non stop

1 Reply

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

Answer by SolidSnake · Jun 10, 2013 at 11:02 AM

Don't you need to call the function as StartCoroutine(attackf("n"));?

Comment
Add comment · 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

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Use yield WaitForSeconds to delay mouseover on a gameObject? (JS) 1 Answer

Coroutines and waiting for a function to finish execution 1 Answer

yield works, but yield WaitForSeconds does not? 1 Answer

StartCoroutine important for using yield? 1 Answer

Im a bit confused on a simple script 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