- Home /
 
destroying a gameobject clone
I am trying to write a script for a null game object to generate a prefab model with a short animation to start off my game but after 5 seconds i need it to be destroyed so that a controllable prefab can spawn in its place. All of this works except that the first model that is spawned will not die. Can someone Please tell me what am I doing wrong?
Here is my code.
//var roverStartUp : GameObject; // Link to the startUp Rover model - not controllable var StartUpAnimation : GameObject; var playerRover : GameObject; // Link to the Player controlled Rover Model
function Start ()
{
Instantiate (StartUpAnimation, transform.position, transform.rotation);
 
               yield WaitForSeconds (5);
 
               Destroy (StartUpAnimation); 
 
               Instantiate (playerRover, transform.position, transform.rotation);
  
               Destroy (gameObject); 
}
Answer by Mike 3 · Dec 17, 2010 at 05:20 PM
You need to store the instantiate return into a variable, then destroy that, right now you're trying to destroy the prefab asset
var instantiated = Instantiate (StartUpAnimation, transform.position, transform.rotation);
 
               yield WaitForSeconds (5);
 
               Destroy (instantiated); 
 
              Your answer