Search Unity

Object with BoxCollider2D seems to block UI interaction

Discussion in 'UGUI & TextMesh Pro' started by liortal, Dec 20, 2016.

  1. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    We have a scene that's made of:
    1. A UI Button
    2. An object with BoxCollider2D (layer: custom).

    The canvas is in "World Space".

    There's currently only 1 camera in the scene, with a Physics2DRaycaster (set to "Everything).

    My problem is that the object with the BoxCollider2D seems to "block" any interaction i want to have with UI objects, even though it is not positioned

    See a better explanation in this screenshot:
    upload_2016-12-21_1-9-1.png

    Note that the button is seen on the canvas, but the object with 2d collider is actually behind it.

    The problem is that the button is not clickable!! even though from what i understand, raycasts from the camera should hit the button, before hitting that object and its colider.

    So, what's going on here? what am i doing wrong ?
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi there,

    you probably have already solved this, but out of curiosity I replicated your setup, and no, not getting the same result here...

    Tested some settings, I guess you could have had negative settings in Canvas components "Order in Layer"... (something like -1000)

    If this is the case, you'll get clicks on 3D object with collider before you get a hit on a button on your world space canvas that is in front of 3D object.
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @liortal

    I was bit too careless when reading your explanation.

    Seems like the issues is having *2D* box collider. If you were to use box collider instead, then the z-position of objWithCollider works like I described... and no need to change anything else I think.

    I tested this with your scene.

    At this point I have no idea how to setup your scene so that it works with 2D box collider.

    EDIT; removed....
     
    Last edited: Dec 25, 2016
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @liortal

    OK. Quite interesting but actually now I think what you can do;

    1. Use 3D box collider and Physic raycaster on your camera. This way you can just move 3D collider object behind your UI, at it works just fine.

    2. If you want to keep your Physics 2D Raycaster and 2D box collider, go to your Graphic Raycaster in Canvas, set the Blocking objects enum; either make it 3D (that way 3D colliders only block UI) or make it 2D, this way 2D objects will block your UI sprites, but you can then set Blocking Mask below it, accordingly; objWithCollider on Layer custom would not be detected if blocking mask is set to Default only...
     
    gareth_untether likes this.
  6. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Thanks for your interest :)
    We already resolved the issue using option #1

    I still have no explanation as to why it didnt work. I may try to debug the UI code to better understand this scenario, when i have some free time.
     
  7. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @liortal

    It's because of the second thing I mentioned. EDIT: no idea if it makes any sense, but it seems to be working like this...

    You'd have to set blocking objects or mask in your Graphic Raycaster.

    Having 2D collider on plane object and 2D Physics Raycaster in camera will allow you to catch a click on plane object using EventSystem, but then again, EventSystem's raycasts will will hit 3D object first (for some reason, I haven't put effort into this enough to know why...).

    Thus you have to set mask in GraphicRaycaster.