Click on a collider behind another one

Hello everyone !

Here’s my problem : i actually have two collider A & B and B is behind A and clearly separated from it. When the player comes in front of A and click, it should normally activate an event of A.
But in fact it’s an event of B which is activated. I am wondering why this is possible and how can i fix it ?

It looks like (with test i’ve made) that i can click all over A but the part that overlaps B makes me click on B.

EDIT1 : it seems that the problem is due to both collider belongs to the same layer. Is there any interference between collider of the same layer ?

EDIT2 : I use NGUI and maybe the problem begins here, with some parameters of the script “UI Cam”…

Ok so i found a way for that, for those who have the same problem, who begins and don’t know where the problem is situated.

The reason why your mouse detect your two colliders when you hover juste one (with the second behind the first) is that your raycast is “unlimited”. The raycast seems to be a line that detect things when clicking with the mouse (that’s what i understand).

To fix this problem (if you’re using NGUI of course) you have to check your MainCamera where is attached your “UICamera” script. There you have an area “Raycast Range” set by default to -1 “unlimited”. It will detect all the things that are on the way of your mouse.
Set up the Raycast range to a bigger number (5,10,20,…) in order to only detect colliders in 5, 10, 20units far from your mouse.

I hope this will help !

Hi, I know this topic is old, but I got to it when searching for a solution myself. So for future reference, there’s an easier/cleaner way.
I think the best approach is to simply move the objects (the ones that are in the way of your mouse clicking) to the “Ignore Raycast” layer.