Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trying to wait for animation to play, then execute code

Discussion in 'Scripting' started by 2close4missiles, Dec 12, 2016.

  1. 2close4missiles

    2close4missiles

    Joined:
    Feb 13, 2015
    Posts:
    13
    So I'm trying to have an animation play when a player hits a health pack. After the animation is finished, the pack should be destroyed. To do this, I am using a coroutine with time equal to the animation time. Instead of waiting the designated time, the pack is destroyed as soon as the player hits it. Two things: Is there a better way to be doing this? And why isn't this method working properly? Please and thanks!

    Code (CSharp):
    1.    
    2.     public int damage;
    3.     public int damageTaken;
    4.     public int currentHP;
    5.     public int maxHP;
    6.     public GameObject target;
    7.     public float waitTime = 5;
    8.  
    9.  
    10.  
    11.     void OnTriggerEnter2D(Collider2D other)
    12.     {
    13.         if ((other.tag == "Player"))
    14.         {
    15.             if (damageTaken > 0)
    16.             {
    17.                 target.GetComponent<Animator>  ().SetBool("isCollected", true);
    18.                 other.GetComponent<PlayerController2D>().addHealth(damage);
    19.                 StartCoroutine (waitForAnim ());
    20.             }
    21.         }      
    22.     }
    23.  
    24.     IEnumerator waitForAnim()
    25.     {
    26.         yield return new WaitForSeconds(waitTime);
    27.         Debug.Log ("Destroy");
    28.         Destroy (target);
    29.     }
    30.  
     
  2. radicalEd

    radicalEd

    Joined:
    Mar 19, 2016
    Posts:
    81
    From the code you posted, the IEnumerator should indeed wait for 5 seconds (unless for some reason you not specifying that 5 was a float (5f) would cause the problem).

    Something else is destroying it, most likely.

    I would suggest using Destroy(this), or if you want to be fancy, Destroy(this, time), where time is the number of seconds to wait. You wouldn't even need to use the IEnumerator then.

    One last thing, it's usually not good to hard code values. waitTime should be set from the actual time the animation takes to complete, which can be obtained with Animator.GetCurrentAnimatorStateInfo(0).length.

    How I think this should be fixed:

    Code (CSharp):
    1.  
    2.     public int damage;
    3.     public int damageTaken;
    4.     public int currentHP;
    5.     public int maxHP;
    6.     public float waitTime = 5;
    7.  
    8.     void OnTriggerEnter2D(Collider2D other)
    9.     {
    10.         if ((other.tag == "Player"))
    11.         {
    12.             if (damageTaken > 0)
    13.             {
    14.                 Animator anim = gameObject.GetComponent<Animator>() as Animator;
    15.                 anim.SetBool("isCollected", true);
    16.                 other.GetComponent<PlayerController2D>().addHealth(damage);
    17.                 Destroy(this, anim.GetCurrentAnimatorStateInfo(0).length);
    18.             }
    19.         }    
    20.     }
    21.  
     
    2close4missiles likes this.
  3. 2close4missiles

    2close4missiles

    Joined:
    Feb 13, 2015
    Posts:
    13
    Problem solved! Thanks for the help!
     
  4. ComradeVanti

    ComradeVanti

    Joined:
    May 22, 2015
    Posts:
    25