- Home /
How to destroy objects only when they collide with other objects?
I have done a gun that automatically fires bullets every "update". The problem I'm having is when I added the script:
void OnCollisionEnter() { Destroy (gameObject); }
the bullets collide with each other and disappear. Is their a way to make it disappear only when it collides with objects other than the bullet?
Any help is very much appreciated.
Answer by unidad2pete · Aug 14, 2017 at 09:36 AM
Edit>Project Settings>Tags and Layers
Add new tag named bullet set tag of your gameObject with the collider to bullet
void OnCollisionEnter(Collision collision)
{
if(collision.collider.gameObject.tag != "bullet")
{
Destroy(gameObject);
}
}
Other option:
Edit>Project Settings>Tags and Layers Add new Layer named bullet and set your bullet with llayer bullet
Edit>Project Settings>Physics
Uncheck square bullet/bullet. Now, colliders with layer bullet, dont have collision with other gameObjects with layer bullet