# Player disappears when it collides with a tagged object

I am working on a simple game where the goal is to help a Player catch specific objects with a tag "Present".

After taking care of the models, animations I am now working on the collisions and counting UI.

For the collisions, on my Player Controller (I am using the ThridPersonUserController from the player Unity Standard Assets - which simplified the whole animation process), I added the following functions:

    void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Present")
{
Destroy(gameObject);
count = count - 1;
SetCountText();
}
}

void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count == 0)
{
winText.text = "Thanks to you the Christmas will be memorable!";
}
}


However, like this, when the Player hits an object with the tag "Present", even though the counting is working properly, the player disappears.

I have tried to change the OnCollisionEnter to an OnTriggerEnter, like this:

    void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Present"))
{
Destroy(gameObject);
count = count - 1;
SetCountText();
}
}


However when the Player hits the objects with the tag "Present", they don't disappear.

My Player has a Capsule Collider and a Rigidbody.

The objects with the tag "Present" have a Box Collider and a Rigidbody.

Any guidance on how to make my Player stay in scene while removing the other objetcs and reducing the count is appreciated.

I think that you should use Destroy(other.gameobject) if you want to destroy other object and not a player.

I was destroying the wrong gameObject. Solved it by changing Destroy(gameObject) to Destroy(other.gameObject).

void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Present")
{
Destroy(other.gameObject); //other.gameObject instead of just gameObject
count = count - 1;
SetCountText();
}
}


Additionally, as Samuel G pointed out, if the goal is to optimize for performance:

Always use CompareTag() which is optimized for performance.

Setting the Collider IsTrigger property well then make use of the OnTriggerEnter events and not the OnCollisionEnter anymore.

So, this is the final solution:

   void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Present"))
{
Destroy(other.gameObject); // this is destroying the other gameobject
count = count - 1;
SetCountText();
}
}