Please Help With playing Animation before destroying a gameobject

Hey guys have a project due friday and the last thing im trying to figure out is how to play an animation before destroying a game object . its basically a key pick up .Ive tried to use invoke etc but with no luck if anyone can help I would be very great full

heres my script .

using System.Collections;

public class KeyPickUp : MonoBehaviour
{
    public AudioClip KeyGrab;                       
	
  
    private GameObject player;                   
    private PlayerInventory playerInventory;      
    
    
    void Awake ()
    {
       
        player = GameObject.FindWithTag("Player");
        playerInventory = player.GetComponent<PlayerInventory>();
		
		
    }
    
	
    
    void OnTriggerEnter (Collider other)
    {
  
        if(other.gameObject == player  this.gameObject.tag == ("KeyCardA"))
        {
         
               AudioSource.PlayClipAtPoint(KeyGrab, transform.position);
            
              
            playerInventory.hasKeyA = true;
			  
		
			animation.Play();
			
			Destroy(gameObject);
            
        }
		
		 if(other.gameObject == player  gameObject.tag == ("KeyCardB"))
        {
        
          AudioSource.PlayClipAtPoint(KeyGrab, transform.position);
            
           
            playerInventory.hasKeyB = true;
            
            animation.Play();
           
			Destroy(gameObject);
		}
		 if(other.gameObject == player  gameObject.tag == ("KeyCardC"))
        {
          
          AudioSource.PlayClipAtPoint(KeyGrab, transform.position);
          playerInventory.hasKeyC = true;
          Destroy(gameObject);
		}
		 if(other.gameObject == player  gameObject.tag == ("KeyCardD"))
        {
           
          AudioSource.PlayClipAtPoint(KeyGrab, transform.position);
            playerInventory.hasKeyD= true;
            Destroy(gameObject);
		}
    }
	

}

Invoke should work. You can then use AnimationClip-length and/or AudioSource-clip before destroying your object.
Instead of directly calling Destroy(gameObject)
you can invoke your IEnumerator function in which you can wait until the animation clip/ audiosource clip length has expired after which the Destroy(gameObject) can get called.

thanks man I really dont know how to use the IEnumerator function its what Im stuck on

Since i prefer Coroutine i am calling the IEnumerator with StartCoroutine and passing the waiting time as a parameter. Alternatively you can use Invoke and get the desired waittime in the ienumerator.

Untested but should work something like that.

using System.Collections;
 
public class KeyPickUp : MonoBehaviour
{
//[...]
    IEnumerator PlayAnimationAndDie(float waitTime)
	{
		animation.Play("animationNameYouHaveUsed");
		AudioSource.PlayClipAtPoint(KeyGrab, transform.position);
        yield return new WaitForSeconds(waitTime);
        Destroy(GameObject)
    }
      
   
	void OnTriggerEnter (Collider other)
	{
		if(other.gameObject == player  this.gameObject.tag == ("KeyCardA"))
		{
			playerInventory.hasKeyA = true;
			 
			StartCoroutine("PlayAnimationAndDie", animation.clip["animationNameYouHaveUsed"].length);
		   ////Invoke("PlayAnimationAndDie",0); then you would have to get the length value in the IEnumerator
		}
	}
//[...]
}

You could try setting a local bool at the start say…

using System.Collections;

public class KeyPickUp : MonoBehaviour
{
    public AudioClip KeyGrab;                       
	
  
    private GameObject player;                   
    private PlayerInventory playerInventory;
    bool killAfterPlay = false;

and then instead of destroying the objet where you are doing it change this to

    void OnTriggerEnter (Collider other)
    {
  
        if(other.gameObject == player  this.gameObject.tag == ("KeyCardA"))
        {
         
               AudioSource.PlayClipAtPoint(KeyGrab, transform.position);
            
              
            playerInventory.hasKeyA = true;
			  
		
			animation.Play();
			
			killAfterPlay = true;
            
        }

then in Update() just check, if this bool is true and the animation isPlaying is false then we can destroy the object.

Thanks guys that worked great , used the bool version and works perfectly , i do however have to get my head around coroutenes really thanks , here`s the working script is anyone wants an answer

 using UnityEngine;
using System.Collections;

public class KeyPickUp : MonoBehaviour
{
    public AudioClip KeyGrab;                       
	public bool isDone = false;
  
    private GameObject player;                   
    private PlayerInventory playerInventory;      
    private bool killAfterPlay = false;

	
	
    void Awake ()
    {
       
        player = GameObject.FindWithTag("Player");
        playerInventory = player.GetComponent<PlayerInventory>();
		
		
	}
	
	void Update()
		
	{
		
		if (killAfterPlay == true  animation.isPlaying == false)
			
		{
			Destroy(gameObject);
		}
	}
	
    
    void OnTriggerEnter (Collider other)
    {
  
        if(other.gameObject == player  this.gameObject.tag == ("KeyCardA"))
        {
      
			
               AudioSource.PlayClipAtPoint(KeyGrab, transform.position);
            
              
            playerInventory.hasKeyA = true;
			  
		 animation.Play(); 
		killAfterPlay = true;
			
		}
			
	   
		
		
			if(other.gameObject == player  gameObject.tag == ("KeyCardB"))
        {
        
          AudioSource.PlayClipAtPoint(KeyGrab, transform.position);
            
           
            playerInventory.hasKeyB = true;
            
             animation.Play(); 
		killAfterPlay = true;
		}
		 if(other.gameObject == player  gameObject.tag == ("KeyCardC"))
        {
          
          AudioSource.PlayClipAtPoint(KeyGrab, transform.position);
          playerInventory.hasKeyC = true;
           animation.Play(); 
		killAfterPlay = true;
		}
		 if(other.gameObject == player  gameObject.tag == ("KeyCardD"))
        {
           
          AudioSource.PlayClipAtPoint(KeyGrab, transform.position);
            playerInventory.hasKeyD= true;
             animation.Play(); 
		killAfterPlay = true;
		}
			
			
			
			
    }
	

	}

Worth noting is that you can actually pass a delay to Object.Destroy. So you could do Destroy(gameObject, animation["animationName"].length); to destroy the object after the length of animation “animationName”.