- Home /
Sorting Layers affecting colliders for OnDrag events
Problem: I have two GameObjects in a scene with colliders that overlap (with different Z coordinate). Then I attach script to them with OnBeginDrag event. This works as expected (dragging on them activates the top one) but when I add some renderer to the object in the back and assign that renderer a different Sorting Layer the OnBeginDrag is now called on this object instead of the front one.
From documentation it seems that Sorting Layers are only affecting rendering pipeline and should not be associated with colliders in any way.
Can someone explain to me why is it working this way?
Steps to reproduce:
add physics 2d raycaster to camera
add Event System to scene
add Standalone Input Module to scene
- create "back" gameobject
add 2d box collider to "back"
add sprite renderer to "back"
create new sorting layer and move it under "default", set this layer to sprite renderer
attach script with IDragHandler, IBeginDragHandler interfaces to "back"
create "front" gameobject with collider and set it's Z coordinate so it is closer to camera
Sample project: [1]: /storage/temp/185091-colliderssortinglayers.zip
Your answer
![](https://koobas.hobune.stream/wayback/20220613041913im_/https://answers.unity.com/themes/thub/images/avi.jpg)