Unity GUI.Button & Mouse Events Not Working

Locking the Cursor Disables Button & Mouse Events

Unity GUI buttons and mouse events such as OnMouseOver or OnPointerDown have their events disabled when the cursor is set to a locked state.

The cursor is locked by setting Cursor.lockState = true; which locks the mouse cursor to the center of the screen.

Even on android or iOS mobile devices where a mouse cursor isn’t used and a touchscreen is instead GUI.Buttons will not be clickable and mouse events will not be called.

NGUI Hide Mouse Cursor Preventing Unity Button & Mouse Events

Be aware that if you’re using NGUI (Next-Gen UI by Tasharen Entertainment) the ‘hide mouse cursor’ option also locks the cursor.

This means that Unity GUI and built in mouse events won’t be called, however NGUI’s own events won’t have any issues with it. Just note that trying to use GUI.Button alongside with NGUI for debug buttons for example will not work unless you unlock the cursor lockState first.

Affected Unity Versions & Confirmed to be Intentional

This functionality was introduced in early versions of Unity 5.4 and was confirmed by the Lead Unity UI Developer over on the Unity forums as being an intentional change by design.

Prior to the change events would still be called when able to interact with the buttons in ways other than the mouse, such as the touchscreen of an android or iOS mobile device.

One Comment on “Unity GUI.Button & Mouse Events Not Working”

Leave a Reply

Your email address will not be published.