Unity3D Destroying animation after its played

So what im trying to do is after the destroy player function was called I want my player to play an animation and then to delete the object and the animation that its played.At the moment it plays the animation and then creates a new player object ,but the animation is still on screen and its not destroyed.Please help me.

function destroyPlayer(dpos : Vector3)
{
var player1 : GameObject = Instantiate(playerDeathObj,dpos,playerDeathObj.transform.rotation) as GameObject;
player1.animation["death"].speed = 3.5;
yield WaitForSeconds(0.5);
Destroy(player1);
}

After running a Debug.Log after the WaitForSeconds i noticed that destroy player is not even reached but if i try to put it on the Update function it destroys my new object.I really dont know what to do.Help me please.

also if i remove the yield WaitForSeconds it destroys it like it should but the animation is not played anymore...

1 Answer

1

playerDeathObj is a reference to a prefab and as such it does not have a transform.rotation, so your Instantiate is wrong.
You need to set the rotation to the transform of something already in your scene.

Destroy can take a second argument that is the wait time, making the yield WaitForSeconds redundant.

Finally. you don’t always need to Destroy things; SetActive (false) is better in most cases.

I did not know Destroy can take wait time argument.Thank you very much that totaly solved my problem :D .