Fluesopp
Fluesopp

Reputation: 93

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

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

}

Upvotes: 3

Views: 30576

Answers (9)

sibasis rath
sibasis rath

Reputation: 1

It may be when you are clicking on the object it is not responding to your clicks. To make it response make sure your simulated box in the rigid body component is enabled. (I faced the same issue with the OnMouseDown().)

Upvotes: 0

hero
hero

Reputation: 1

If you use the new input system, you need to add PhysicRaycast to your camera.

Upvotes: 0

Vlad Headmaster
Vlad Headmaster

Reputation: 1

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

Upvotes: 0

Tone
Tone

Reputation: 39

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.

Upvotes: 3

Graham Paterson
Graham Paterson

Reputation: 1

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.

Upvotes: 0

user393267
user393267

Reputation:

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.

Upvotes: 0

Aizen
Aizen

Reputation: 1825

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.

Upvotes: 0

Drewen
Drewen

Reputation: 2936

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!

Upvotes: 9

Remon Ramy
Remon Ramy

Reputation: 177

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

Upvotes: 4

Related Questions