Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
9
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 On$$anonymous$$ouseDown.

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

Show more comments

18 Replies

· Add your reply
  • Sort: 
avatar image
7
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 On$$anonymous$$ouseDown() function responded. Thanks a bunch!

avatar image
20

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 5 · 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 On$$anonymous$$ouseDown

And the function name is On$$anonymous$$ouseDown and not on$$anonymous$$ouseDown

Zero$$anonymous$$cm

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

Thanks Zero$$anonymous$$cm,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 On$$anonymous$$ouseDown() to work.

avatar image nabilzaman1 · Feb 25 at 09:00 AM 0
Share

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.

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

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. on$$anonymous$$ouseDown only fires if the object is close enough to the camera.

avatar image
2

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.

Comment
Add comment · 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
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 @$$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.

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

Follow this Question

Answers Answers and Comments

38 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 avatar image

Related Questions

Passing OnMouseDown to other Colliders 2 Answers

OnMouseDown() intended behaviour 1 Answer

Simple Camera Movement?! (javascript) 1 Answer

Each consequtive mouse click removes a different Gui.Texture? 1 Answer

How can I prevent an invisible collider from intercepting an OnMouseDown() event? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges