I have 2 objects, both have a Box collider 2D.

The first object is on the Level1 Sorting Layer and has a Tile script attached to it.

The second object is on the Level0 Sorting Layer and doesn't have any scripts.


The Tile script code is as follows:

void OnMouseDown()

Whenever I click the top tile, nothing prints in the console unless I click on the red area like I show in the image below (red area = [first box collider] minus [the two box colliders intersection area]).

enter image description here

Why this happens ? Why doesn't the OnMouseDown() function fire up simply when you click on its collider, since the top tile is on a higher sorting layer than the bottom tile. How does this work ? Doesn't sorting layer affect the 2D box collider ? (If I change the Z-axis of the top tile to -1, while keeping the bottom tile to 0, it works as expected.)


If you have stacked BoxCollider2Ds like that, Unity will only capture the mouse click on the topmost one.

You could:

  • Adjust the z-layer of your colliders (I think you do this by setting the renderer's "Order in Layer").

  • Enable/Disable your colliders in code based on which one is on top.

  • Do a RaycastAll, and process the click yourself.

  • \$\begingroup\$ what do you mean by "Unity will only capture the mouse click on the topmost one" ? how do you define the topmost ? this is what i am trying to achieve here. that's the point of the sorting layer ? at least i thought it was. apparently it is not. it seems like it only affects the drawing order. not the collider order as well. the order in layer is useless since the 2 objects are in the same sorting layer. so the only option is adjusting Z-layer, it is going to be hell when you have complex sorting layers and order in layers like in my game \$\endgroup\$ – dimitris93 Apr 12 '15 at 14:42
  • \$\begingroup\$ @Shiro Order In Layer of your renderer will determine the layer of your collider. At least that is how I remember it working. Like I said, you could use RaycastAll, and simply check all the objects that are returned from that raycast, and action the one with the highest layer value. \$\endgroup\$ – jgallant Apr 12 '15 at 15:19
  • \$\begingroup\$ order in layer effects objects of the same layer. my objects are in different layers in the first place. I have already made this work by linecasting. i feel like this shouldn't be the way though. i liked your idea of enabling/disabling colliders in code. how efficient is that though ? I feel like unity might not be happy about changing it on runtime every 10 seconds. \$\endgroup\$ – dimitris93 Apr 12 '15 at 15:27
  • \$\begingroup\$ I don't think disabling/enabling a collider is very demanding resource wise. You can always run some tests if it is a concern, but I would think it is fine. In fact, it may even speed things up considering that a bunch of your colliders wouldn't get updated. \$\endgroup\$ – jgallant Apr 12 '15 at 15:33
  • \$\begingroup\$ I guess it has to be better than having 5 times more colliders and them being always on. I ll try it out \$\endgroup\$ – dimitris93 Apr 12 '15 at 15:34

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.