Search Unity

Why won't my collectible item destroy itself?

Discussion in 'Getting Started' started by smatpat, Jul 27, 2020.

  1. smatpat

    smatpat

    Joined:
    Jul 27, 2020
    Posts:
    13
    I've followed a Unity tutorial to add a collectible to my demo game but the item just won't destroy itself.

    My collectible item has Rigidbody2D and a Box Collider 2D applied. It's set as a trigger.

    My player object is tagged as "Player".

    Here's my code:

    Code (CSharp):
    1. namespace smat
    2. {
    3.  
    4.     public class Collectible : MonoBehaviour
    5.     {
    6.  
    7.         private void OnTriggerEnter(Collider other)
    8.         {
    9.             if (other.tag == "Player")
    10.             {
    11.                 Destroy(this.gameObject);
    12.             }
    13.         }
    14.  
    15.     }
    16. }
    Can you spot an issue here? I swear it's identical to the tutorial.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Add Debug.Log statements. Verify OnTriggerEnter is being called, verify that it sees the correct tag.
     
    smatpat likes this.
  3. smatpat

    smatpat

    Joined:
    Jul 27, 2020
    Posts:
    13
    I can now confirm it's not recognising the OnTriggerEnter (or recognising the tag)

    Not sure why...?
     
    Last edited: Jul 27, 2020
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Verify it appears that the objects are actually colliding, or otherwise entering the trigger collider. Verify the collider is set to trigger mode. Verify the rigidbodies and colliders are configured so that OnTriggerEnter should be called. See the below link, scroll down to the bottom, and verify in the table.

    https://docs.unity3d.com/Manual/CollidersOverview.html