Unity C# OnMouseDown() does not work.. Why?

Fluesopp picture Fluesopp · Mar 7, 2015 · Viewed 17.8k times · Source

I have an issue with the OnMouseDown() event. The object this script belongs to is a computer cabinet which can be clicked to execute certain actions. But i'm stuck right on square 1.. I have tried to search the web for this issue for some time now, but allmost nobody seems to have the same issue, and other solutions have not been working perfectly. Can anybody please help me? I'm confused..

public class ComputerScript : MonoBehaviour 
{
    private RotateForDisplay displayRotation;
    private GameObject displayObjects;

    private void Start()
    {
        displayObjects = GameObject.Find("DisplayObjects");
        displayRotation = displayObjects.GetComponent<RotateForDisplay>();
    }

    // This does not seem to work for some reason..
    private void OnMouseDown()
    {
        displayRotation.isRotating = false;
        Debug.Log("Mouse is down");
    }

}

Answer

Drewen picture Drewen · Mar 7, 2015

Check the following points:

  • Check your target has a collider (this system works like using a raycast) and it is enabled
  • Check if the collider has not been resized or moved.
  • Check if you do not have any other object with a collider inbetween.

Hope it helps, good luck!