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 Kyieon · Jan 02, 2014 at 11:52 PM · c#coroutineyieldienumerator

Waiting twice inside coroutine (C#)

Hey guys, I am working on a system for changing weapons.

I need it to wait for the disarm animation to finish before playing the arm animation for the next weapon. However it does not make it to the Debug.Log statement as it exits the routine after the yield. Any other ideas on how to get the routine to wait twice?

 using UnityEngine;
 using System.Collections;
 
 public class weaponManager : MonoBehaviour {
     private bool busy = false;
     public Transform currentWeapon;
     public Transform[] weapons;
     public int startWeapon = 1;
 
     void Start(){
         StartCoroutine(EquipWeapon(startWeapon));
     }
 
     void Update(){
         if(!busy){
             if(Input.GetKeyDown(KeyCode.Alpha1)){
                 StartCoroutine(EquipWeapon(1));
             }else if(Input.GetKeyDown(KeyCode.Alpha2)){
                 StartCoroutine(EquipWeapon(2));
             }else if(Input.GetKeyDown(KeyCode.Alpha3)){
                 StartCoroutine(EquipWeapon(3));
             }
         }
     }
 
     IEnumerator EquipWeapon(int weaponNumber){
         busy = true;
 
         if(currentWeapon != null && currentWeapon != weapons[weaponNumber - 1]){
             Transform weapon;
             weapon = currentWeapon;
             weapon.GetComponent<weaponScript>().armed = false;
             weapon.animation.Play("Disarm");
             currentWeapon = weapons[weaponNumber - 1];
             yield return WaitForAnimation(weapon);
 
             Debug.Log("passed");
             weapon = currentWeapon;
 
             weapon.animation.Play("Arm");
             yield return WaitForAnimation(weapon);
             weapon.GetComponent<weaponScript>().armed = true;
         }else if(currentWeapon == null){
             Transform weapon = weapons[weaponNumber - 1];
             
             weapon.animation.Play("Arm");
             yield return WaitForAnimation(weapon);
             weapon.GetComponent<weaponScript>().armed = true;
         }
 
         busy = false;
     }
 
 
     private static IEnumerator WaitForAnimation(Transform obj){
         do{yield return null;}while(obj.animation.isPlaying);
     }
 }
 
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

2 Replies

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

Answer by Key_Less · Jan 03, 2014 at 12:12 AM

Try yielding for the duration of the animation inside the EquipWeapon coroutine itself.

 var clipLength = weapon.animation.GetClip("Disarm").length;
 yield return new WaitForSeconds(clipLength);
 
 // Do your stuff...
 
 clipLength = weapon.animation.GetClip("Arm").length;
 yield return new WaitForSeconds(clipLength);
Comment
Add comment · Show 5 · 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 Kyieon · Jan 03, 2014 at 12:44 AM 0
Share

I've tried that, it might work in javascript but im trying to stick to c#.

avatar image Key_Less · Jan 03, 2014 at 12:52 AM 0
Share

Whoops, just noticed I missed the 'new' and I've edited the answer. It's written in C# and should work like a charm :)

avatar image Kyieon · Jan 03, 2014 at 02:06 AM 0
Share

I did it with the "new" it runs the coroutine fine but returns after, im trying to pause the the routine without returning

avatar image Key_Less · Jan 03, 2014 at 02:52 AM 0
Share

It does return indeed, this is how coroutines work. At the yield instruction, control is returned back to Unity and any further execution of the coroutine is paused until the next frame. WaitForSeconds further delays any execution for the given time period and once the delay has been met, the coroutine will begin execution from the last executed yield instruction.

I would recommend reading this article on coroutines. There is a good example of a coroutine that waits for an animation to partially complete. It's written in JS but can very easily be converted into C# and modified so that it waits for the entire length of the animation.

If this doesn't sound like the desired result, then you may want to look else where other than coroutines.

avatar image Kyieon · Jan 03, 2014 at 08:16 AM 0
Share

Thanks:) I will take a look

avatar image
0

Answer by rutter · Jan 02, 2014 at 11:53 PM

Close, but not quite right:

 yield return WaitForAnimation(weapon);

Yielding on another coroutine has a tricky syntax in C#, since you need to use StartCoroutine():

 yield return StartCoroutine(WaitForAnimation(weapon));
Comment
Add comment · Show 1 · 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 Kyieon · Jan 03, 2014 at 12:03 AM 0
Share

It doesn't pause the coroutine that calls it.

I'm trying to pause the same coroutine twice.

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

20 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 avatar image avatar image avatar image avatar image

Related Questions

Yield Wait for Seconds (A little help here) 2 Answers

Why Won't My Coroutine Yield? 2 Answers

Returning an IEnumerator as an int? 1 Answer

What am I doing wrong with IEnumerator? 1 Answer

Turn bool on/off x time every few seconds in coroutine 2 Answers


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