- Home /
OnMouseDown not firing?
I have a project with an ortho camera. Added a new cube and attached a script with an OnMouseDown method to it but it doesn't get called when clicking the mouse on the cube.
The cube has a collider, the scene is empty except for the cube(so no invisible objects grabbing the raycast), and the script is attached to the cube. Also, the cube is on the default layer (not on the ignore raycast layer). What am I missing?
For reference, here is the code, "started" gets logged but not OnMouseDown:
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.LogError("started");
}
void OnMouseDown() {
Debug.LogError("down");
}
}
Using latest Unity 4.x.
Is Trigger is not necessary...that's only if you want the collider to behave like a trigger, and has no effect on On$$anonymous$$ouseDown.
@radlemur: I put the script on a cube in the setup you describe and it works as expected.
I tried this in a fresh project and it worked fine. Still not working in the original project. I wonder what else could be causing it to not register...
maybe you said that your cube should ignore something in your original project?
I doubt it, this was initially a particle system with a sphere collider and then switched to a new scene in the same existing project that only had the camera and the cube. $$anonymous$$aybe it's something at the project level.
I have since switched to using plain Raycast, but will leave this open as I'm curious what other reasons could there be for the click to not register...
Answer by vividhelix · Oct 28, 2013 at 10:36 PM
This may be caused by many things, but if you've checked all the causes and can't find anything, it's probably caused by the distance from the camera. The OnMouseDown implementation uses a depth limit for raycasting which may cause the object to not register the clicks.
Also, it won't work if you are clicking on something that is not seen by the main camera.
@vividhelix I tried what you said and it worked! I just moved my objects a little closer to camera and the On$$anonymous$$ouseDown() function responded. Thanks a bunch!
Answer by robertbu · Aug 21, 2013 at 04:26 PM
Usually when I see this problem it is due to a collider conflict. That is, OnMouseDown() is raycasting under the hood. If the ray from the mouse position strikes another collider (visible or not), you don't get the OnMouseDown() call. To debug put the following code on an empty game object. Take a look at what is returned when you click on something that should generate an OnMouseDown() but does not:
using UnityEngine;
using System.Collections;
public class RaycastExample : MonoBehaviour {
void Update () {
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
Debug.Log ("Name = " + hit.collider.name);
Debug.Log ("Tag = " + hit.collider.tag);
Debug.Log ("Hit Point = " + hit.point);
Debug.Log ("Object position = " + hit.collider.gameObject.transform.position);
Debug.Log ("--------------");
}
}
}
}
Collider might be missing in this case. Add collider to the cube and try.
You need to add a collider to your cube, it's necessary to use On$$anonymous$$ouseDown
And the function name is On$$anonymous$$ouseDown and not on$$anonymous$$ouseDown
Zero$$anonymous$$cm
Thanks Zero$$anonymous$$cm,your answer resolved my issue :).
Thanks man, the code helped me identify exactly why I kept failing to get On$$anonymous$$ouseDown() to work.
This is such an awesome debugging tool. This helped me find my problem in a minute after over an hour of searching through the forums. Note: This uses the "old" input system. You need to make sure that's enabled to get this to fire.
Answer by Glorion13 · Jun 05, 2013 at 04:05 PM
Hi radlemur,
I had the very same issue with you. I still do not understand the deeper issue. What I tried at first was create a new object, apply the script (with no changes) and see if it works. It didn't work. What I tried next was restarting Unity and it still didn't work. Finally, what I did was delete all objects that referenced that script (including prefabs). Then I created the same objects anew, applied the very same script (no changes whatsoever) and it started working. Since in all cases I was basically working with the default Spheres and Cubes, I think this might be a bug.
No need for raycasting for simple behaviour like that in my opinion, these default events should work (and now they do!).
I hope this helps.
Cheers, Alex
I too had a similar issue. I had a game object with a script using On$$anonymous$$ouseDown that worked fine in one scene. I pulled it out from the scene as a prefab, saved, opened another scene and added it. On$$anonymous$$ouseDown just wouldn't work. After trying everything else suggested in this post, I deleted the imported game object from the new scene, saved, closed Unity, re-opened the scene, added back my prefab and it worked.
Yes, I just fixed this issue with a image plane I was using as a button and I moved the image or object closer to the camera and shrunk it to fit the size I wanted on screen and it worked. on$$anonymous$$ouseDown only fires if the object is close enough to the camera.
Answer by diggerjohn · Jul 22, 2015 at 01:41 PM
Thank you all for your input. So many angles on this. Of course I did mark this answered way back but failed to include what the actual answer was. The solution on my part is totally out of left field. This problem was happening only in the editor when playing my app. It turns out it was my Wacom Pad Mouse that was not being recognized. The cursor will move but clicks are not being registered. When I plugged in a USB wired mouse it works just fine. I have updated the drivers for the Wacom Tablet but it hasn't changed the effect. So I am just keeping an old USB Mouse around for development.
Answer by MehmetOguzDerin · Aug 19, 2013 at 01:08 PM
Hello,
Try this :)
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
void Start () {
Debug.Log("started");
}
void Update() {
if (Input.GetMouseButton(0))
Debug.Log("down");
}
}
0 = LMB
1 = RMB
2 = MMB
Hi @$$anonymous$$edalOf$$anonymous$$ode that will not do what we want to do, that code will be called whenever the mouse click anywhere on the screen, not specifically on the GameObject that the script belong to.
Your answer
Follow this Question
Related Questions
OnDestroy collision detect 4 Answers
Stop a Lerp from looping 3 Answers
OnMouseUp error 0 Answers
Do something when there is no mouse click 1 Answer
endless sidescroller porting to android 0 Answers