having code pause while animation plays.. yield not working

Hunting around on the forums here i thought I found how to using yield to have animation play before the rest of the code executed ,… though I have this bit of code and it seems to totally ignore the yield return WaitForSeconds(animation.clip.length); call…

  void Update()
    {
       
			if((!firing)  (!moving))
			{
				if(Input.GetKeyDown("space"))
				{

                    StartCoroutine(followPlayerScript.SetAttack());
                
					firing=true;
					moving=true;

                                      ..start code that starts some other logic and action of projectiles etc... dont want to do this
                                   ... until the above is done animating.

				}
                      }
}

THen the function called:

public IEnumerator SetAttack()
    {
        anim.CrossFade("Staff Spell Forward");


        staff.renderer.enabled = true;
        Debug.Log("*********************** ANIM LENGHT :  " + anim.clip.length);
        yield return new WaitForSeconds( anim.clip.length); //should be almost 1 second, even tried 10f and still no pause...
  

    }

Am I missing something? Maybe the forum posts I found are older and not updated for the latest version of unity (4.3 something) I am running?

Have you tried

if(Input.GetKeyDown("space")){
 firing=true;
 moving=true;

}

if((!firing)  (!moving)){
   StartCoroutine(followPlayerScript.SetAttack());
}

I don’t have Unity with me rigfh now…so I could be wrong

hmm I didn’t think that StartCoroutine might have to be in its own block… though the problem wouldn’t be solved above…at least not by reworking the logic. Ideally the attack animation happens on space key then the projectile is spawned

Just wanted to bump this, tinkering with it now and still cannot make it work for me :frowning:

This is how you should be using yield with a coroutine. When you start a coroutine it is essentially running concurrently with the code it was called from.

  void Update()
    {
      
            if((!firing)  (!moving))
            {
                if(Input.GetKeyDown("space"))
                {

                    StartCoroutine(followPlayerScript.SetAttack());
               
                   

                                      ..start code that starts some other logic and action of projectiles etc... dont want to do this
                                   ... until the above is done animating.

                }
                      }
}

THen the function called:

public IEnumerator SetAttack()
    {
        anim.CrossFade("Staff Spell Forward");


        staff.renderer.enabled = true;
        Debug.Log("*********************** ANIM LENGHT :  " + anim.clip.length);
        yield return new WaitForSeconds( anim.clip.length); //should be almost 1 second, even tried 10f and still no pause...
 
         firing=true;
         moving=true;

    }