why does this not work
void OnCollisionEnter2D(Collision2D col)
Just to make that clear (since) neither the question mentions it and the additional comments aren’t specific enough. You have:
- A Rigidbody2D (not a Rigidbody) on the object that is moving.
- The object that moves does have some kind of Collider2D on it and that collider is not marked as “trigger”
- The object you expect to hit with your moving object does also have a Collider2D on it that is not a trigger.
- 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.
- 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.
- Of course the objects are actually enabled / active
- 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