If you collide with an object destroy the object updated for unity 5 ?

I am trying to make it so that when i collide with an object it destroys the objects and adds to my inventory.

To all of the unintelligent people who posted C#, read the tags and question next time. He also said when you collide with it, not when you enter a trigger.

Anyway if you’re wanting it to just destroy what it has hit then do this:

function OnCollisionEnter (col : Collision){
   Destroy(col.gameObject);
}

but if you would like to destroy the parent then you would do this:

function OnCollisionEnter (col : Collision){
   Destroy(col.transform.root.gameObject);
}

now you may need to tweak some stuff instead of Destroying the object if you want to add it to your inventory, but this is different for everyone and I don’t know how your inventory is set up.

What exactly do you need? C# Code?

You could do it for example like this in a script on the player:

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Item"))
    {
        //.... code to add other.gameObject to inventory

        Destroy(other.gameObject);
    }
}

The object you want to pick up, should have a collider, with “Is Trigger” on true and in this case the tag “Item”.

void OnTriggerEnter (Collider coll)
{
// Here you can Add the coll GameObject to your Inventory (perhaps a
// dictionary)
Destroy (coll.transform.gameObject);

}