- Home /
stupid question
Destroy (Clone)
I'm creating a bullet script and the only thing that I cant figure out is destroying multiple cloned objects. Ive gotten as far as destroying one of them but after more are spawned the script cannot catch up.
bullet.js
var bullet : Rigidbody;
var speed : float = 10.0f;
var muzzlePoint : Transform;
function Update() {
if(Input.GetButtonDown("Fire1")) {
var instance : Rigidbody = Instantiate(bullet, muzzlePoint.position, muzzlePoint.rotation);
instance.velocity = muzzlePoint.forward * speed;
Destroy(GameObject.Find("Bullet(Clone)"), 5);
}
}
ParticleSystem $$anonymous$$yPS = Instantiate(YourParticle, position, Quaternion.identity); Destroy($$anonymous$$yPS.gameObject, 1f);
Answer by bubzy · Sep 02, 2013 at 04:18 PM
GameObject[] killEmAll;
killEmAll = GameObject.FindObjectsWithTag("Bullet");
for(int i = 0; i < killEmAll.Length; i++)
{
Destroy(killEmAll[i].gameObject);
}
i think (c#)
ORRRR you could have
float bulletLife = 5.0f;
Destroy(gameObject,bulletLife); //pretty sure this will destroy it after 5 seconds.
attach this to the bullet.
hey, the second script worked! thanks so much!
but there's a problem; it deletes the original bullet also so when I shoot again, there is nothing to clone or spawn. is there a way that I can only delete or shoot the clones?
I did it without using prefabs using this code
function Update() {
if(gameObject.name == "Bullet(Clone)"){
Destroy(gameObject, 5);
}
}
Actually all he needs is:
Destroy(instance.gameObject, 5);
This will autodestruct the bullet after 5 seconds. Using any "Find" method where is just a waste of cpu power.
Follow this Question
Related Questions
Issues with destroying a game object 2 Answers
Cannot destroy instantiated objects 2 Answers
Bullet not dissapearing after time ? 3 Answers