- Home /
 
How to create a delay before playing an animation
I'm new to Unity and c#. I've been hours trying to figure this out so I apologize if it's a repeat question, if there is I haven't found it yet.
I have two animations. One plays when the player collides with an object, the other when the colliders aren't touching anymore. I want to create a delay between the first animation ending and the second animation beginning.
In my code below I used void OnCollisionExit2D but this caused the second animation to play immediately I then tried to play around with the transitions in the animation window however I couldn't get the delay to happen.
I then tried to use WaitForSeconds(10) however I am unable to use this within a void function.
If I can't create a time delay within my onCollisionExit function, is there another way I can cause this delay to happen before playing the second animation? I simply just want the first animation to trigger, then after 10 seconds, have the second animation trigger.
My code is below, Thanks in Advance!
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class causeColorChange : MonoBehaviour {
 Animator myAnim;
 public GameObject player;
 // Use this for initialization
 void Start()
 {
     myAnim = GameObject.Find("Player").GetComponent<Animator>();
 }
 void OnCollisionEnter2D(Collision2D Coll)
 {
     if(Coll.gameObject.tag == "Player")
     {
         myAnim.SetBool("isBlue", true);
     }
 }
 void OnCollisionExit2D(Collision2D Coll)
 {
     if(Coll.gameObject.tag == "Player")
     {
         
         myAnim.SetBool("isBlue", false);
     }
 }
 // Update is called once per frame
 void Update () {
     
 }
 
               }
Answer by thunderbuns · Aug 01, 2018 at 12:27 AM
I believe that your looking for WaitForSeconds which can be found here.
Your answer