3
\$\begingroup\$

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;
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Vadim Tatarnikov Jul 30 '15 at 7:13
  • \$\begingroup\$ 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. \$\endgroup\$ – OwenAlexander Jul 30 '15 at 8:33
  • \$\begingroup\$ 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. \$\endgroup\$ – Vadim Tatarnikov Jul 30 '15 at 8:38
  • \$\begingroup\$ 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 :) \$\endgroup\$ – OwenAlexander Jul 30 '15 at 9:13
4
\$\begingroup\$

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);                       
             }
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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