How can I get both colliders' types in a trigger overlap message like OnTriggerExit2D(Collider2D other)?


  • objects A and B both have a BoxCollider2D and CircleCollider2D components.

  • Then, object A enters one of the trigger colliders of object B.

  • I can get object B's collider type with the is operator.

How can I get A's collider type?

The reason I want this is that I want only collisions where object A's CircleCollider2D touches object B's BoxCollider2D.

void OnTriggerExit2D(Collider2D other)
    if (other is BoxCollider2D)
        //Do Code

Here is a table of what I want: I want only to register trigger overlaps from CircleCollider2D to BoxCollider2D. With the is operator, I can safely remove all collisions to CircleCollider2D but collisions between two BoxCollider2D stay, which I do not want.

|  Row to Column   | BoxCollider2D | CircleCollider2D | 
| BoxCollider2D    | No            | No               | 
| CircleCollider2D | Yes           | No               | 

Edit The real questions was to detect nearby objects without the detection range dectecting each other.

  • 1
    \$\begingroup\$ Have you considered using physics layers to prevent the unwanted collider pairs from interacting in the first place? \$\endgroup\$ – DMGregory Jun 25 '19 at 21:46
  • \$\begingroup\$ Yes, at the moment, I'm already in another physics layer. What I am trying to do is to know what objects are in a certain range of my object. I have a circle collider for the range and a box collider of the shape of the object. \$\endgroup\$ – RegularNormalDayGuy Jun 25 '19 at 22:02
  • 1
    \$\begingroup\$ Sounds like you want a Physics2D.OverlapCircleNonAlloc, not a CircleCollider2D \$\endgroup\$ – DMGregory Jun 25 '19 at 22:08
  • \$\begingroup\$ oh, lol that does exactly what i need. Thanks a lot (if you want to put it as answer, I will upvote) \$\endgroup\$ – RegularNormalDayGuy Jun 25 '19 at 22:14
  • 1
    \$\begingroup\$ I recommend editing your question to ask the question you're really trying to answer ("how do I detect nearby objects without my detection ranges detecting each other") then write your solution as an answer. \$\endgroup\$ – DMGregory Jun 25 '19 at 22:17

With Physics2D.OverlapCircleNonAlloc, you can set an area around a point (my gameobject) and then saving the results in a Collider2D[] array which contains all objects nearby from the range I want.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.