Second coin is destroyed when i touch the first one

Well I have a script that when the player touch a coin it will play a sound, an animation and then it will be destroyed.

I have a playermodel and two coins on a row, i grab the first coin and get 1 score, the animation plays and is then destroyd, then i grab the second coin but it gives me:

“MissingReferenceException: The object of type ‘Animation’ has been destroyed but you are still trying to access it.”

Script:

#pragma strict

var score 	= 0;
var coinSound: 	AudioClip;
var coinPickAnimation: Animation; 

function Start () {

}

function Update () {

}

function OnTriggerEnter (other:Collider) {

	if(other.gameObject.tag == "coin"){
		score++;
		audio.PlayOneShot(coinSound);
		coinPickAnimation.animation.Play("coinpicker");
		yield WaitForSeconds (0.5);
		Destroy (other.gameObject);
		
	}

}

function OnGUI () {

	GUI.Label(Rect(10,10,200,200),"score : " + score);
}

What to do?

It does not look like you are accessing the animation on the coin you trigger. If the coinPickAnimation.aniation.Play(coinPicker") explicitly calls to the first coin it will give you the Missing Reference Exception because you are trying to access the animation component inside the destroyed coin.

What I recommend is moving part of this script to the coin in an OnTriggerEnter function.

function OnTriggerEnter(other:Collider){
     if(other.gameObject.tag == "Player"){
          anims.Play("coinpicker");
          yield WaitForSeconds(0.5);
          Destroy(this.gameObject);
     }
}

That way you can still have the player’s script deal with the ui and the coin deals with itself.