2
votes

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 ThirdPersonUserController 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.

2
Are you trying to make the player disappear with Destroy(gameObject);? - Ruzihm
I am trying to make the object, with the tag "Present" disappear when the player collides with it. - Gonçalo Peres 龚燿禄
Destroy(gameObject); has nothing to do with the present GameObject. Maybe you mean to use Destroy(other.gameObject);? Also, if your collider is not set to be a trigger, you should be using OnCollisionEnter, not OnTriggerEnter - Ruzihm

2 Answers

2
votes

Your present object is using a non-trigger collider, so you should use OnCollisionEnter. The CompareTag call you tried in OnTriggerEnter is preferable. You should use that.

Also, you are currently trying to destroy the gameobject that the ThirdPersonUserController is attached to (gameObject). Instead, destroy the gameobject of the colliding collider (other.gameObject) with Destroy(other.gameObject);:

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

Few things. You are destroying the incorrect game object:

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

Update to:

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

Always use CompareTag() which is optimized for performance.

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

On the first physics update where the collision is detected, the OnCollisionEnter function is called. During updates where contact is maintained, OnCollisionStay is called and finally, OnCollisionExit indicates that contact has been broken. Trigger colliders call the analogous OnTriggerEnter, OnTriggerStay and OnTriggerExit functions.

See the docs