Why OnTriggerEnter2D doesn't work

Hey guys, I need help

I have created a small demo in Unity 2d, but when trying to destroy the game objects (basic game logic is - destroy moving objects) that are moving from above - OnTriggerEnter2D is not destroy it. Looks like it’s not on a similar layer.

DetectCollisions.cs

public class DetectCollisions : MonoBehaviour
{
    void Start()
    {

    }
    void Update()
    {
        // Destroy an objects
    }
    void OnTriggerEnter2D ( Collider2D other )
    {
        Debug.Log("Enemy");
        Destroy(gameObject);
        Destroy(other.gameObject);
    }
}

What can be the issue? Maybe someone has similar things



Could be a many number of things, your immediate setup looks fine (spelling, types, capitalisation, and the configuration seen in your screenshots) so you’ll have to go through some troubleshooting steps like mine to figure out what you’re doing wrong.

I think the problem is that you may have the Rigidbody’s Body Type set to Static or Kinematic. I’m not sure why, but I think Unity doesn’t detect if any of those Body Types are selected, if you don’t want your Enemy to move, you should set their gravity to 0. Maybe that fixes the issue. You should add a Collider and a Rigidbody to both objects. Hope this helps :smiley: