Skip to main content
6 of 7
added 51 characters in body
dimitris93
  • 1.3k
  • 2
  • 16
  • 27

Unity sprite with higher sorting layer not firing its OnMouseDown function

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.

1

The Tile script code is as follows:

void OnMouseDown() { Debug.Log(Input.mousePosition.ToString()); } 

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 ?

dimitris93
  • 1.3k
  • 2
  • 16
  • 27