Issue with Tags and CompareTag and OnTriggerEnter2D

Hey all, I am having issues with collider and comparing tags. The collision works fine, rigid body, is set as a trigger. Once i add the IF, it doesnt seem to work. I cant find the answer online, usually to bad tags but all mine appear ok.

car is the player (rigid body and collider appear to work)
package is the pick up (Collder set as trigger, no rigid body_

private void OnTriggerEnter2D(Collider2D collision)
{
    //Debug.Log("Zoom");
    //This gives the proper output
   
    Debug.Log(collision.gameObject.tag);
        //Returns "Untagged"
        //The game object has the proper tag in editor
          
    if (collision.gameObject.CompareTag("Package"))
    {
        Debug.Log("zoom");
        //Nothing happens

    }
}

thanks all!

check the name of the object to check its looking at the right thing of whats hitting the trigger

You don’t provide important information here, namely where is this code snippet located? Is it in the “Delivery” script? If so, the collider provided in that callback clearly isn’t going to be the one on the “Package” GameObject with the “Package” tag, it’s going to be whatever is triggering it i.e. your “car”.

If this script was on the “car” then it’d get the callback informing you that it hit the “package”, assuming you’ve got everything else set-up correctly.

As above, don’t focus on tags, output the GameObject names instead to give you more information.

That just means it is another GameObject. Print it’s name! Find out who it is! Debug this thing!!! Print out ALL THE THINGS! :slight_smile:

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

1 Like

Thanks guys, This helped alot!

1 Like