Not Destroying on Collision JS

Im trying to make it so when 2 Objects collide it destroys one of them but for some reason they move through eachother so the Box Colliders hit eachother but its not picking up them colliding.

They are on the same layer and are both untagged here is my code

var collision: Collision;
var GameObjectName: String;



function Start () {

}


function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.name==GameObjectName) {

Debug.Log("Please Work");
Destroy(gameObject);

}

}

The game Object Name is set to the right name

Any ideas - sorry if it doesnt make alot of sense im not very good at communicating.
Please and Thankyou :slight_smile:

So some simple things that might be the issue:

  • Are you sure both objects have Colliders
  • Are you sure at least one of them has a RigidBody (with IsKinematic NOT checked)
  • Are you sure they aren’t Triggers (TriggerBox checked)
  • IF this is a 2D game and you have 2D colliders and RigidBodies you need OnCollisonEnter2D

If all of these are true, then try adding a Debug.Log before the if statement to see if the method is firing at all:

function OnCollisionEnter(theCollision : Collision){
Debug.Log("Collided with " + theCollision.gameObject.name);
if(theCollision.gameObject.name==GameObjectName) {

Then you can test if your name is correct.

Yeah its colliding

Okay haha got it to work but its only destroying the monster not the bullet O.o

It will currently destroy whatever the script is attached to.

If it’s on the monster, then you want to destroy theCollision.gameObject.