0

I am trying to under how "Raycast Target" option works. I think the it works is

  1. if this is checked, then my UI element will consume the mouse/touch events and any 3D object behind it will not get the mouse/touch event.
  2. If Unchecked, this will pass the mouse event to 3D object behind it.

but his is not how it is behaving.

I have a UI text on canvas and a 3D cube gameobject behind it.I have added following script on UI text

public class CaptureUIObjectEvent : MonoBehaviour, IPointerClickHandler
{
    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("In UI click event");
    }
}

I have added following script on 3D object

public class Capture3DObjectEvent : MonoBehaviour 
{
    void OnMouseDown()
    {
        Debug.Log("In 3D click event");
    }
}

When "Raycast Target is checked" : Both the UI text and 3D object is capturing the event. So both the logs are printed. The UI text is not blocking the click event. I think this is not the correct behavior. Also the 3D object event log is printed first. I think Unity has multiple default raycaster in scene for UI (graphics raycaster) and 3D (Physics raycaster) objects. Is it so? Is this behavior expected then and does this mean my 3D object will get the mouse click always?

When "Raycast Target is Unchecked" : Only 3D object is getting clicked. This is expected I think.

I have to make the CaptureUIObjectEvent derive from IPointerClickHandler to make it capture any event but for 3D object I dont have to use that. I thibk this is also because of different raycaster used by UI and 3D objects. Is it so?

This is my scene enter image description here

Pankaj Bansal
  • 889
  • 1
  • 12
  • 37
  • @Programmer: The question you mentioned does not explain why "Raycast Target" when checked does not block the event – Pankaj Bansal Jun 16 '17 at 08:19
  • Are you sure you don't have both raycasters (a graphic one and a physic one) in your scene? That would explain why your cube gets the mouse click event. – Kardux Jun 16 '17 at 08:48
  • That answers answers this *"Is this behavior expected then and does this mean my 3D object will get the mouse click always?"* If you want blocking to work, don't mix `OnPointerClick` and `OnMouseDown`. Use `OnMouseDown` for all your Objects. That answer provided a proper way to actually detect click on any type of object which should solve your problem. – Programmer Jun 16 '17 at 10:20
  • When I have "OnMouseDown" in UI elements, I am not able to get any click event on UI items whether my "Raycast Target" is checked or not. Can you please what mistake I may be making. I am able to get click event when I use "OnPointerClick" – Pankaj Bansal Jun 16 '17 at 12:18
  • Sorry. That was a typo when I said *"Use OnMouseDown for all your Objects."*. I meant to say use `OnPointerClick` and `OnPointerDown` for all your Objects. See #6 and #7 from the duplicated answer on how to make this work on 2D and 3D objects. You can also find other functions to use from that post. – Programmer Jun 16 '17 at 17:08

0 Answers0