OnTriggerEnter2D not working

Hello,

I am completely new to Unity, and trying to learn how collision system works. Unfortunately in my case, it is not working :).

I have a basic 2D game layout and two objects. One is a player game object with Rigidbody2D component attached to it, the second one is a basic cube, with box collider and is trigger → checked on.

Now I simply want to check the collision between these two so part of my player script for collision looks like this:

void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log("Collision");
    }

However it does nothing. Is there anything else I need to do? It looks to me like something is not set-up properly in the inspector. (both objects have position o Z axis set to: 0 - is this correct?)

Thank you

Hello Michal,
Did you remember to add also a collider component to your players game object? In order for the collison to work, you will need a collider on both, your player and the cube.

Also make sure, that you are using 2D colliders (e.g. BoxCollider2D and not BoxCollider), otherwise OnTriggerEnter2D will not be invoked :wink:

Thank you :). The problem was, I was not using BoxCollider2D but just BoxCollider. Now it works like a charm.

1 Like