- Home /
explosion instaniate not destroyed
i have a 2d space shooter. The bullet prefab is contains a script and attached component is explosion transform. when my bullet hits asteroid it instantiates the explosion and it is destroyed but i still have explosion clones Why are they not destroyed since they are attached to the bullet prefab and bullet objects are destroyed
my Bullet script
var bulletSpeed : float = 15.0; //bullet speed
var vertMax : float =10; //height of bullet
var explosion : Transform;
function Start () {
}
function Update () {
transform.Translate (0, bulletSpeed*Time.deltaTime, 0);
if (transform.position.y>=vertMax){
Destroy(gameObject);
}
}
function OnTriggerEnter(other:Collider){
if (other.gameObject.tag=="asteroid"){
other.transform.position.y=10; //reset "other" object (asteroid)
//transform.position.x= Random.Range(7,-7);
//create explosion
Instantiate (explosion, transform.position, transform.rotation);
Destroy(gameObject);
}
}
Firstly, where are you attaching the explosion to the bullet? Secondly, it sounds like you're expecting the explosion to disappear as soon as the bullet does, which would result in it being created and destroyed instantly in this code.
bullet is attached to another game object. Im trying to destroy the explosion prefab clone, but it wont let me destroy assets. I have tried assigning a variable to explosion transform and other things but wont work.
If i could destroy it id just have to put a delay in to see the effect
Answer by emperor1ee · Oct 04, 2013 at 01:57 PM
i have since found a solution. Attach a script to expolsion gameobject and destroy it after time period
function Update () {
Die();
}
function Die(){
yield WaitForSeconds (waitTime);
Destroy(gameObject);
}