How to destroy an object on collision with ANYTHING.

I’m trying to make a gun, but I’d like the bullets to be destroyed whenever they hit anything.

Current code is:
using UnityEngine;
using System.Collections;

void OnCollisionEnter(Collision a)
{
    Destroy(this);
}

This doesn’t work. I’ve tried other stuff, like destroy gameObject, but that doesn’t work either. What would work? Am I forgetting a step?

Are you saying I should add a force to the wall to push back? Sorry, I'm fairly new to Unity.

1 Answer

1

this is the script component. If you want to destroy the gameobject to which the component is attached, try this instead:

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

Maybe you could make a quick sketch of what it is exactly you want to happen? If you want something specific to happen when hitting the wall you could do it in the callback OnCollisionEnter2D(Collision2D collision) void OnCollisionEnter2D(Collision2D collision) { // If this Hits a Collider with the tag name MyWall if (collision.collider.CompareTag("MyWall")) { // Do whatever should happen on wall collision } }