Destroy bullet on random collision

I’m quite new to scripting. How would I go about this? I want my bullet do be destroyed at any collision so not a specific collision.

2 Answers

2

You’ll have to use

Destroy(gameObject);

when your bullet hits a object. I am guessing that the bullet is a rigidbody so using OnCollisionEnter would not be a bad idea. Your code might look something like this:

function OnCollisionEnter(){
    Destroy(gameObject);
}

Hey there!

In unity, there are built-in methods that you can call from within your script. Make a new c# script called “Bullet”.

in Bullet, add this below the Update function

void OnCollisionEnter(Collision c)
{
    GameObject.Destroy(this.gameObject);
}

however, in order for this to actually work, you must make sure that:

a) Your bullet object has a rigidbody and a collider attached to it

b) All the moving objects you want your bullet to collide with also have both rigidbodies and colliders

c) All the static objects(not moving, such as buildings, mountains, etc) have colliders attatched to them.

Hope this helps!