3

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");
    }

}
2
  • 2
    The first sentence of the tag unity is "DO NOT USE ON QUESTIONS ABOUT THE UNITY GAME ENGINE (use: unity3d instead)!!" Mar 7, 2015 at 19:03
  • Please don't include start or update when they are empty. Aug 13, 2018 at 1:47

7 Answers 7

7

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!

1
  • When I first met this problem, the reason was an absence of collider.
    – 0leg
    Jan 2, 2016 at 11:12
3

Try this:

void OnMouseOver(){
if(Input.GetMouseButtonDown(0){
displayRotation.isRotating = false;
Debug.Log("Mouse is down");
}
}

You might also want to use void Awake() instead of void Start():

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

These functions are so easy to use, but their implementation is fraught with hidden cases that make use of them problematic.

For instance, if object C is set up with a collider but has no Rigidbody, and is the child to object P that HAS a rigidbody, the parent object P will have its methods called when you interact with C, and C's methods will not be called.

This undesireable behavior occurs even if Object P has no collider at all.

What does a Rigidbody (which has no bounds) have to do with mouse input? Nothing at all.

The only fix that occurs to me is to sprinkle all your mouse-aware objects with useless Rigidbodies (gravity=false and kinematic, perhaps) to avoid having their mouse events stolen. What is the overhead cost in doing so? It's a mystery.

Unity's idea for this is very clever and simple, but it seems to be implemented poorly.

0

Does it give you the "Mouse Down" Message for the Debug log? If yes then the problem might be in your RotateforDisplay Class.

Which is not included in your question.

0

I am having the same issue, and the root cause seems to be the number of objects that has a collider on it, between your camera and the object you want to click.

It is based on raycasting in the end, so if there is anything in between, chances are that the ray is not hitting the game object.

0

One thing is to check is make sure the box collider size properties surround the object you wish to get the click event for as that was what was stumping me.

0

OnMouseDown() works only when you click on object to which it attached

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.