Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I cannot understand why the coroutines are not waiting the designated number of seconds before executing the code below it. At the moment, the animation bleeds into the next state. I could get around this by using .time and if statements but I thought the whole point of coroutines is that you can avoid that sort of mess.

         public IEnumerator LoadBow()
     {
         this.gameObject.GetComponent<Animation>().CrossFade("Load_Bow"); 
         yield return new WaitForSeconds(this.gameObject.GetComponent<Animation>()["Load_Bow"].length); 
         aimStates = AimStates.Draw; 
     }

     public IEnumerator DrawBow()
     {
         this.gameObject.GetComponent<Animation>().CrossFade("Draw_Bow"); 
         yield return new WaitForSeconds(this.gameObject.GetComponent<Animation>()["Draw_Bow"].length); 
         aimStates = AimStates.Aim; 
     }

     public void Aiming()
     {
         moveTarget.SetActive(false); 
         switch(aimStates)
         {
             case AimStates.Load:
                 StartCoroutine(LoadBow()); 
             break;

             case AimStates.Draw:
                 StartCoroutine(DrawBow()); 
             break;
share|improve this question
    
You could try and use booleans with the coroutines and do checks, try creating a variable of type bool and set the boolean to true of false before the yield AND after. Then just use if statements to check specific booleans, this will allow you to wait certain amount of time and then run your code. –  Vadim Tatarnikov Jul 30 at 7:13
    
I'm just a little confused as I thought coroutines were useful because you could dispense with booleans and if statements. I've made the code work by just dispensing with the coroutines and using my old if statement method using animation[].time as the bool. I was hoping there was a more efficient way. –  OwenAlexander Jul 30 at 8:33
    
Well I told you my way of doing it and it works fine for me. So as I said you could set a Boolean for example to false before yield and then after the time had passed set it back to true and then just use if statement to check if the Boolean is true/false. What about efficiency, I think there's not much of a difference. From what I understand the delay from coroutine affects only something you change before and after the delay so that's why I use booleans, I find it less complicated I guess and more freedom of code. –  Vadim Tatarnikov Jul 30 at 8:38
    
Thanks for the info. Sounds like a good strategy. I just wanted to minimize bools as I've accumulated many bugs due to excessive reliance on them. This looks like a good use of them though so I'll give it whirl :) –  OwenAlexander Jul 30 at 9:13

2 Answers 2

up vote 3 down vote accepted

If you call coroutines this way they will be executed in order, one after another. You should fire them up with StartCoroutine(Aiming());

    public IEnumerator Aiming()
             {
                 yield return StartCoroutine(LoadBow());
                 yield return StartCoroutine(DrawBow());
                 aimStates = AimStates.Aim;
             }       


        public IEnumerator LoadBow()
             {
                 aimStates = AimStates.Load;
                 this.gameObject.GetComponent<Animation>().CrossFade("Load_Bow"); 
                 yield return new WaitForSeconds(this.gameObject.GetComponent<Animation>()["Load_Bow"].length);                      
             }

             public IEnumerator DrawBow()
             {
                 aimStates = AimStates.Draw;
                 this.gameObject.GetComponent<Animation>().CrossFade("Draw_Bow"); 
                 yield return new WaitForSeconds(this.gameObject.GetComponent<Animation>()["Draw_Bow"].length);                       
             }
share|improve this answer

You could set a boolean for example to false before yield and then after the time had passed set it back to true and then just use if statement to check if the boolean is true/false. What about efficiency, I think there's not much of a difference. From what I understand the delay from coroutine affects only something you change before and after the delay so that's why I use booleans, I find it less complicated I guess and more functional.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.