- Home /
OnMouseDown() Doesn't work
Hello, I usually never have problems with this, but somehow I can't get the OnMouseDown() to work. This is my code:
using UnityEngine;
using System.Collections;
public class Menu : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseDown() {
Debug.Log("Clicked");
}
}
I have tryed with other stuff instead of Debug.Log("Clicked"); but it doesn't detect the click at all.
I have a collider on my gameobject. The script is attached to the same gameobject as the collider.
I don't know what can be causing this, but that's why I ask here ;)
Thanks, Andreas :-)
Thank you sooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo much robertbu :D :D I forgot all about checking if anything else was in the way :) Please convert to answer, so I can "Accept" it :) :D
Answer by robertbu · Jul 04, 2013 at 07:26 PM
Make sure the script is attached to the game object you want to be clickable.
Make sure you have an enabled collider
Make sure you have not resized or changed the position of the collider
Make sure there are no other colliders in the way. OnMouseDown() works just like a Raycast()...the first thing hit is what gets the OnMouseDown().
If everything looks okay and it still does not work, delete the object and start over by adding the new object, adding the script...
Answer by Turbine · Apr 25, 2014 at 02:12 AM
Necro post for prosperity:
OnMouseDown works completely differently if there is a RigidBody somewhere in the hierarchy. It actually won't call OnMouse functions on the clicked object but it will instead call them on the RigidBody's game object instead!
In pseudo code:
void UnityApiMouseEvents()
{
RaycastHit hit;
if ( Physics.Raycast(hit))
{
if ( hit.rigidbody != null )
hit.rigidbody.gameObject.SendMessage("OnMouseDown");
else
hit.collider.SendMessage("OnMouseDown");
}
}
I've been looking for this for ages! Thanks! Why is that not mentioned on the Docs??
Yes, thank you for necro-posting. It is 2017 and the difference in behavior introduced by a rigidbody in the hierarchy is still not mentioned in the documentation.
Answer by NateJC · Dec 20, 2016 at 04:37 AM
For UGUI UI objects, instead of OnMouseDown, use OnPointerDown:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems; // Required when using Event data.
public class ExampleClass : MonoBehaviour, IPointerDownHandler // required interface when using the OnPointerDown method.
{
public void OnPointerDown (PointerEventData eventData)
{
Debug.Log (this.gameObject.name + " Was Clicked.");
}
}
Alternatively, this can also be handled in the Inspector by adding an Event Trigger component.
Answer by anthodb · Jan 16, 2015 at 10:53 AM
Just to add my little part in this, I was having OnMouseDown not firing at all on my gameObject. Did try everything for about one hour to finally close Unity (4.5.5) and re-open. That did the trick !!
Thanks, anthodb, this just happened to me, and your recommendation to close and reopen Unity fixed it.
I am running Unity v4.6.0f3 on Windows 8.1. Wonder if this is a known bug...
No idea why but this worked for me as well EDIT: worked once then stopped working forevermore.
Answer by bakinto · May 20, 2020 at 03:58 AM
This may seems so stupid, but make sure you click on your game window and not the view!! that's what happened with me, and almost lost it before I realise im clicking on th view not the game widow :T lol
Your answer
Follow this Question
Related Questions
GUI Repeatbutton up? 1 Answer
Help with resize script 1 Answer
Knowing when mouse has been released 2 Answers
gui.button down 2 Answers
FPS camera mouse script - look around only when Mouse button is pressed down? 1 Answer