Deleting a bullet
Hi! I have a machine gun in my game that have a FireRate of 0.1 (waits 100 milliseconds before shooting), and it shoots bullets with the speed of 100 (i guess Kmph).
My bullets are PLANES and they have rigidbody AND colliders, and i am using a script to delete them at any cost, here it is:
#pragma strict
var Coll = false; //Ignore this var, i don't even know why i made this, i am going to delete it soon...
function update()
{
yield WaitForSeconds (10);
Destroy (gameObject);
}
function OnCollisionEnter(collision: Collision) {
if (Coll == true) {
Destroy (gameObject);
}
}
function OnCollisionExit(collision: Collision) {
Coll = true;
}
Sorry for the poor explanation, i sincerely don't know how to explain this properly. As you see in my script, it is supposed to make my bullets be destroyed when they touch ANY collider, and even if they don't, after 10 seconds they were supposed to be destroyed. The problem is that the bullets are ignoring the script. The bullets aren't being destroyed after 10 seconds, and some of them still continue existing even after hitting a collider. I have set the bullet prefab's collision detection to continuous dynamic and STILL it doesn't do anything. Can someone help me here? What am i missing?
Answer by JincSoft · Oct 25, 2016 at 06:36 PM
#pragma strict
function Start()
{
Destroy (gameObject, 10f);
}
function Update()
{
}
function OnCollisionEnter(collision: Collision) {
Destroy (gameObject);
}
The code currently requires the bullet to collide with something, exit the collision bounds, then collide with something else before it would be destroyed. The Update function needs a capital letter (Update() not update()) and you can move all that code to the Start() function since it only needs to be called once.
Nice! I didn't even noticed that i typed u ins$$anonymous$$d of U. I expected Unity to give me some error or something if it was wrong. I deleted and rewrited the script so fast that i didn't noticed the error. Anyway, thanks!
Answer by hexagonius · Oct 25, 2016 at 03:49 PM
Update, not update
Correct. In addition: Your code will only destroy the bullet on its second collision as it is only deleted when Coll
is true which it becomes when exiting a collision.
Answer by JoshDangIt · Oct 25, 2016 at 04:25 PM
Don't use Update(), put this in Start()
Destroy(gameObject, 10f); //destroy after 10 secs
Your answer
Follow this Question
Related Questions
Bullet Destroy 1 Answer
The bullet never destroyed!!! 2 Answers
Firing bullets ( Tnx for the help ) 1 Answer
Destroying Bullet (Impact Effect) 1 Answer
MY effect dont destroy 0 Answers