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.
Answer by Wolfrik_Creations · May 10, 2016 at 07:44 PM
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.
Answer by Tim0theus · May 08, 2016 at 04:29 PM
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".
Answer by Dext · May 08, 2016 at 04:29 PM
void OnTriggerEnter (Collider coll)
{
// Here you can Add the coll GameObject to your Inventory (perhaps a
// dictionary)
Destroy (coll.transform.gameObject);
}