# Unity Collider2D - Know the collider type ob the object collider with the other object

I have my GameObject which represents a soldier in my game. It has the following colliders:

• 1st BoxCollider2D in child named "sword". Is Trigger checked. Tag = Sword. This collider is enable when player attacks and disabled otherwise.
• 2nd BoxCollider2D in parent GameObject. Is Trigger unchecked, since it's the body. Always enabled.

When the sword (1st) collider collides with the the body (2nd) collider, damage happens. Until now everything works fine. Here is the OnTriggerEnter2D function I have:

void OnTriggerEnter2D(Collider2D collision)
{
if (collision is BoxCollider2D)
{
Debug.Log($"{this.name} collided with {collision.name}"); if (collision == null || collision.tag == Constants.TAG_WEAPONS || collision.tag == Constants.TAG_SIGHT) { return; } if (collision.GetComponent<LivingObjects>().AreEnemies(this)) { Debug.Log($"{this.name} attacked with {collision.name}");

Attack(collision.GetComponent<LivingObjects>());
}
else
{
return;
}
}
}


Now I want my unit to search for enemies within a said range. I added a new collider:

• 3rd CircleCollider2D in child named "sight". Is Trigger checked. Tag = Sight. Always enabled.

Now, when the sight (3rd) collider collides with the body collider, damage is trigger into the unit colliding with the sight collider. That is because I block damage one way around with the condition if (collision is BoxCollider2D) but not the other collision happening at the same moment.

If i can discrimate collision from CircleCollider2D into BoxCollider2D, I can remove the attack function. However, I cannot see how to do it. My question is how do I know what Collider2D type is my object colliding with the other ?

I'm open to other suggestion if this is impossible.

## 1 Answer

I think you should use a layering system of unity and kept the relevant colliders in the same layer as your no. 3rd collider used for searching make a new layer name it "searching" and then player collider and enemy collider keep both in that layer("searching") and then go to Edit-> project settings -> physics2d settings that will show layer collision matrix then uncheck all other irrelevant layers I hope it will solve your problem.

• Yeah, I'm guessing I have no choice to refactor my gameobject architecture. I tried changing the layer physics, but at the moment, every collision was handled in one script (in the parent of the go). So I guess I'll split everything with 3 objects, being the hitbox collider, weapon collider and sight collider. – RegularNormalDayGuy Jun 22 '19 at 15:49
• yes definitely, it will work best of luck – user3699039 Jun 23 '19 at 17:43