Home > database >  OnTriggerEnter2D is not detecting
OnTriggerEnter2D is not detecting

Time:10-25

I have this empty object with a collider attached, and it follows the mouse around. The "is trigger" box isn't checked, and that's it.

Now, on my other Object, I also added a collider (2d of course) and set it as "is trigger" = true, but I also tried it with the box unchecked.

The following Code should execute when the Trigger "area" is entered:

public void OnTriggerEnter2D(Collider2D collider)
{
    print("found coin");
    manager.AddCoin();
    Destroy(gameObject);
}

But it does not. How do I fix this?

CodePudding user response:

I think that you need one of your objects to have a Rigidbody2D attached to it in order for the physics engine to really engage and detect collisions (even triggers).

The documentation seems to imply that:

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html

"Note: Trigger events are only sent if one of the Colliders also has a Rigidbody2D attached. Trigger events are sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions."

CodePudding user response:

It sounds that you need a RigidBody2D attached to your first gameObject (the one you're moving around). To detect collisions, at least one object must have this component (get more info in the Docs)

  • Related