• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
8
Question by vividhelix · Mar 26, 2013 at 10:19 PM · onmousedown

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.

Comment
Add comment · Show 14
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Mar 26, 2013 at 10:49 PM 0
Share

do you have "Is Trigger" selected on the cube?

avatar image Eric5h5 · Mar 26, 2013 at 10:56 PM 2
Share

Is Trigger is not necessary...that's only if you want the collider to behave like a trigger, and has no effect on OnMouseDown.

@radlemur: I put the script on a cube in the setup you describe and it works as expected.

avatar image vividhelix · Mar 28, 2013 at 05:05 PM 0
Share

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

avatar image $$anonymous$$ · Mar 28, 2013 at 09:47 PM 0
Share

maybe you said that your cube should ignore something in your original project?

avatar image vividhelix · Mar 28, 2013 at 09:57 PM 0
Share

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. Maybe 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...

Show more comments

18 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MorphVGX · May 06, 2015 at 02:49 AM 0
Share

Also, it won't work if you are clicking on something that is not seen by the main camera.

avatar image hira-tehreem · Aug 03, 2016 at 06:35 AM 0
Share

@vividhelix I tried what you said and it worked! I just moved my objects a little closer to camera and the OnMouseDown() function responded. Thanks a bunch!

avatar image
2

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GavinF · Dec 17, 2013 at 11:43 PM 0
Share

I too had a similar issue. I had a game object with a script using OnMouseDown that worked fine in one scene. I pulled it out from the scene as a prefab, saved, opened another scene and added it. OnMouseDown 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.

avatar image sunseeker1988 · Sep 03, 2016 at 02:31 PM 0
Share

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. onMouseDown only fires if the object is close enough to the camera.

avatar image
0

Answer by IndieStar · Aug 19, 2013 at 12:43 PM

Hi everyone, I had this very particular issue once, the real issue here is the Z depth of your box collider, for me Z = (0 to 1) will fire the OnMouseDown but higher values will not do, this is ideal for 2d games requires no depth as in my case but for 3D youll have to figure what is really going on.,

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JavierIvan · Jan 13, 2017 at 08:33 AM 0
Share

I have the same issue, and working on a 2D project, I am using a 2D Colider,

It has no Z axis to configure obviously and "It Triggers" is enabled, I have set all objects to position z=0 and the camera to z=1, still dont get OnMouseDown() to work...

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IndieStar · Aug 21, 2013 at 04:06 PM 1
Share

Hi @MedalOfMode 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.

avatar image
17

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 ("--------------");
              }
         }
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Santosh Patil · Oct 29, 2013 at 06:05 AM 0
Share

Collider might be missing in this case. Add collider to the cube and try.

avatar image ZeroKcm · Nov 10, 2013 at 07:46 AM 4
Share

You need to add a collider to your cube, it's necessary to use OnMouseDown

And the function name is OnMouseDown and not onMouseDown

ZeroKcm

avatar image ylhyh ZeroKcm · Jun 20, 2016 at 03:18 AM 0
Share

Thanks ZeroKcm,your answer resolved my issue :).

avatar image TheGameLearner · Sep 12, 2018 at 07:04 AM 0
Share

Thanks man, the code helped me identify exactly why I kept failing to get OnMouseDown() to work.

  • 1
  • 2
  • 3
  • 4
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

37 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnMouseDown to return object name 3 Answers

On Mouse Down to trigger OnGUI function 2 Answers

Reverse Animtion On clicking text. 2 Answers

How to turn on/off multiple lights using GUI buttons? 1 Answer

Conflicting Box Colliders 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges