UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

physics

[Input System] OnMouseDown Fails: Why Your Clicks Aren't Registering

Solution

input systemraycastingcollisionproject architecture

Unity 2019.x - Unity 6.3.x

Published Tue, Mar 10

Issue

 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.

Explanation
  1. Open Project Settings by navigating to Edit > Project Settings.
  2. Select the Player category and scroll to the Other Settings group.
  3. 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:

  1. Use Camera.main.ScreenPointToRay to generate a ray from the mouse position into the 3D scene.
  2. Perform a Physics.Raycast to detect the specific Collider under the cursor.
  3. Execute logic on the hit object by accessing its specific components.

Additional Tips

  • Objects must have a Collider or Collider2D component to receive mouse events.
  • Ensure the Camera does 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.Raycast and that your object has a Collider2D component.

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.