why does this not work

why does this not work

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

Just to make that clear (since) neither the question mentions it and the additional comments aren’t specific enough. You have:

  1. A Rigidbody2D (not a Rigidbody) on the object that is moving.
  2. The object that moves does have some kind of Collider2D on it and that collider is not marked as “trigger”
  3. The object you expect to hit with your moving object does also have a Collider2D on it that is not a trigger.
  4. The two objects that should collide can actually collide. So you haven’t put them on seperate layers and disabled collisions between those layers. See the layer matrix in your settings.
  5. The code you posted is actually part of a MonoBehaviour script that is either attached to the object with the Rigidbody2D or attached to the object you want to hit that has the collider.
  6. Of course the objects are actually enabled / active
  7. Finally the object you’re moving that should hit that other object doesn’t move too fast so it could “phase through” the object in a single frame.

Check those points. If any point does not apply, it would not work. If something I said isn’t clear, feel free to leave a comment.

Check if you add rigidbody component in (at least) one gameobject related to the collision