Collision On Specific Object= Destroy not working.

Hello, so I put together a script that would destroy the object that has the script if it hits the player. However, the object does not break. The script is

#pragma strict

function OnCollisionEnter (hit : Collision)
{
if(hit.transform.gameObject.name == “Player”)
{
Destroy(gameObject);
}
}

My game is 2D. The name of the player is simply Player. I am quite new to Scripting and Unity so all help is appreciated.

should be OnCollisionEnter2D :slight_smile:

Rename your function:

//function OnCollisionEnter (hit : Collision)
function OnTriggerEnter2D(hit : Collider2D)    // if using 2D Colliders
function OnTriggerEnter(hit : Collider)        // if you're using normal Colliders

Both game objects need a Collider (Such as BoxCollider2D).

The non-player collider will need “Is Trigger” to true. Non-player object will also need your script.

The player object will need a RigidBody, set “Is Kinematic” to true;


Colliders as Triggers Tutorial

OnCollisionEnter Tutorial