[Input System] OnMouseDown Fails: Why Your Clicks Aren't Registering
Solution
Unity 2019.x - Unity 6.3.x
Published Tue, Mar 10
The OnMouseDown method fails to invoke on instantiated game objects with Collider components because the Active Input Handling system is restricted to the New Input System or is incorrectly configured in Project Settings. This mismatch prevents the processing of legacy input events required for OnMouseDown functionality, effectively halting anticipated object interactions.
- Open Project Settings by navigating to Edit > Project Settings.
- Select the Player category and scroll to the Other Settings group.
- Change Active Input Handling to Both or Input Manager (Old) to enable legacy mouse events for
OnMouseDown.
Alternatively, implement a raycast system within your script to bypass legacy limitations and gain more control:
- Use
Camera.main.ScreenPointToRayto generate a ray from the mouse position into the 3D scene. - Perform a
Physics.Raycastto detect the specificColliderunder the cursor. - Execute logic on the hit object by accessing its specific components.
Additional Tips
- Objects must have a
ColliderorCollider2Dcomponent to receive mouse events. - Ensure the
Cameradoes not have any UI elements with Raycast Target enabled blocking the scene interaction. - Confirm that the object is not on a layer specified to be ignored in the global Physics settings.
- If using 2D, ensure you use
Physics2D.Raycastand that your object has aCollider2Dcomponent.
TL;DR
If OnMouseDown ignores clicks, verify Active Input Handling settings or implement Physics.Raycast to ensure consistent interaction across all input backends.
Related Posts Haven't quite found a solution to your problem? We think these posts might help you.
Content inspired by a Unity discussion post.