OnTriggerEntered2D/OnCollisionEntered2D refuse to work for me :(

I have a 2D game with lines and sprites. Everything has a Polygon2D collider on it, and I am trying to detect when they collide so I can take action.

However no matter what I do, the OnTriggerEntered2D and OnCollisionEntered2D callbacks never get called, even when there clearly is an intersection. I have tried adding the callbacks to the GOs on both sides, with no luck.

Here is an image showing two objects with Polygon2D colliders clearly intersecting:
Link to full size: Imgur: The magic of the Internet

My callback code is nothing but a debug.log statement.

    void OnTriggerEntered2D(Collider2D col)
    {
        Debug.Log("OnTriggerEntered");
    }

    void OnCollisionEntered2D(Collision2D col)
    {
        Debug.Log("OnCollisionEntered");
    }

Clearly I must be doing something wrong, but I have no idea what. Can anyone help?

If you’re using collision, I think you need rigidbodies2d on both. If you are using trigger, I think the one that isn’t the trigger needs a rigidbody2d

Thanks. I added Rigidbody2D components to both objects but still not getting any callbacks.

I think for now I am just going to write my own intersection routines. Annoying that I can’t figure this out though :frowning:

From my understanding if the object is moving, put a rigidbody on it. You can detect collisions by adding a collider( box, sphere, poly).

Use the trigger option to select between a normal collision or a trigger ( the object can pass through).

Also your code above is wrong.

void OnCollisionEnter2D(Collision2D coll)
{

}

void OnTriggerEnter2D(Collider2D other)
{

}
1 Like

haha. I didn’t even notice that when I looked at his code. Yep, that would be why it’s not working as well.

1 Like

It’s easy to make that mistake, I’ve done it. Visual Studio doesn’t pickup those functions in autocomplete, not sure if mono develop does… never used it.