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 :wink:

Thanks, Andreas :slight_smile:

  • 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…

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

}

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.

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 !!

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

As I understand the GameObject associated with this script must have Collider, Collider2D or GUI component.

  • 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…

here my solution plz tell me if it’s optimazied or not

 if (myCollider.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition)))
                        { OnMouseDown(); }

Just as an addition, if you’re looking to move objects you’ve made in Maya (fbx prefabs) you have to attach the colliders and the scripts to the innermost poly transform in order to actually move the objects. I finally figured that out for myself :slight_smile:

I struggled with this for an hour or so before deleting the old object and reconstructing it (using the same script, unedited) and it worked fine. There might be some obscure reason it doesn’t work, try reconstructing your object.

I have the problem : OnMouseDown() do not fired for a Panel in Canvas, after add a UI/Button component to this panel, assign a function to the On Click() in the Button component , it worked.

There’s yet another trick about this, sometimes when you save the script for some reason the changes don’t take place in unity even if the code does work, to be sure generate an error somewhere, miss a ; for example and then save the file, go back to unity so unity shows you the error on the script, then fix it and save the script again, magically you’ll see the script begins to work, can’t say why this happens but works many times.

hint: if your game in world space in your canvas then :
make sure that the width and the height of the game object you want to click equal the size of the collider(x,y) because the collider size when created it is in pixels, not world size.

Another possibility is that the object you’re trying to click is blocked by an invisible UI object. Make sure all of your invisible UI panels have their Image “Raycast Target” unchecked.

I also found in 2D that you might need to change the Z position of the object you’re trying to click to ensure it’s in front of other gameobjects.
E.g. if your background is on Z axis 0, change your clickable Z axis to -1

You might have moved the sprite but not the game object’s transform, so be careful there

I’ll come in here from 2021 and drop one more necro post tip to this thread.

If you’ve tried everything else and it doesn’t seem to work still, make sure that the active camera in your scene has a Physics Raycaster component on it. We’re using Cinemachine and for some reason the active camera didn’t have that (whether the fault of Cinemachine or us who can say ¯\(ツ)/¯). As soon as I added it, everything started working like a charm.

Edit: Caveat that this seems to only work with the IPointer implementation approach, not the OnMouse.

All the best to you!

For me the problem was that I had one Background object (also with collider2d) on the same layer as my object to click. I put the background object into the “IgnoreRaycast” Layer and that solved the trick

For archival reasons, I ran into a similar issue but I had the mesh renderer disabled and didn’t realize that it was backwards. A plane, for instance, is only visible on one side and that is also the only side that can be clicked on.

  1. create scene object and make shure it has a collider

  2. create 3 materials(inactiveMaterial, HoverOverMaterial, OnlickActiveMaterial)

  3. create a script and add it to the gameobject

  4. add this to the script:

    public Material matInactive, mathover, matActive;
    private  MeshRenderer rend;
    
    private void Awake()
    {
        rend = GetComponent<MeshRenderer>(); // grabs the meshrenderer directly from the object that hold the script
        rend.material = matInactive;
    }
    private void OnMouseOver()
    {
        if(Input.GetMouseButton(0))
        {
            rend.material = matActive;
        }
        else
        {
            rend.material = mathover;
        }
    }
    
    private void OnMouseExit()
    {
        rend.material = matInactive;
    }
    
  5. add the materials to the obejct with the script

  6. press play and test try it out