1
\$\begingroup\$

Basically the title says it all. I have a OnMouseEnter() function that works fine when I have the canvas render mode on Worldspace but stops working when I change it to Camera Overlay.

Before, it would work with just the script with the mouse functions on it attached to the image object. Now to work I need to use an event trigger and add the script to another object to put it into the trigger.

Any idea why?

\$\endgroup\$
1
  • \$\begingroup\$ Before, it would work with just the script with the mouse functions on it attached to the image object. Now to work I need to use an event trigger and add the script to another object to put it into the trigger. \$\endgroup\$
    – ClaudioA
    Commented Jan 5, 2019 at 15:01

1 Answer 1

3
\$\begingroup\$

OnMouseEnter is a MonoBehaviour message that's fired by rays cast in the 3D game world (including hit-testing 2D colliders in that 3D game world too).

Once you set your UI to Overlay, it's no longer in the 3D game world. It exists as a pure UI layer painted over-top by a separate pass at the end.

To catch mouse events in this pure UI world, you need to use the UI's event system approach:

public class UIMouseOverExample : MonoBehaviour, IPointerEnterHandler {

    public void OnPointerEnter(PointerEventData eventData)
    {
        // Do your stuff.
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .