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

Answer by musicartslao · Nov 10, 2013 at 05:54 AM

I have a similar problem in a cube whitout a collider, thought. Here is my snippet:

 #pragma strict
 
 //1 var to track the rotation state and speed
 var rotationState: boolean = true;
 var rotationSpeed: float = 20;
 
 
 function Start () {
 // changes the shader color at runtime
 transform.renderer.material.color = Color.red;
 
 }
 
 function Update () {
 
     if (rotationState == true){
         transform.Rotate (0, rotationSpeed * Time.deltaTime, 0);
     }
 
 }
 
 //2
 function onMouseDown() {
     if (rotationState == true){
         rotationState = false;
     }else if(rotationState == false){
         rotationState = true;
     }
     
     print ("Rotation State =" + rotationState);
 
 }
Comment
Add comment · Show 3 · 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 ZeroKcm · Nov 10, 2013 at 07:44 AM 0
Share

@musicartslao

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 musicartslao · Nov 10, 2013 at 08:07 AM 0
Share

No colliders ! I found the error in the update function: function Update () {

 if (rotationState){
 transform.Rotate (0, rotationSpeed * Time.deltaTime, 0);
 }

This works nicely !

avatar image ZeroKcm · Nov 10, 2013 at 08:37 AM 0
Share

I don't understand your solution... What do you change?

avatar image
0

Answer by Oldskewl · Jan 17, 2014 at 02:22 PM

I realize this is marked as answered already but in hopes of helping others who run into this same issue check one more thing off the list of possible causes:

check if the item you are clicking is inside another object's collider.

e.g. I had this occur when I used a short-wide cylinder gameobject as a platform that my "clickable" gameobject stood upon. By default the cylinder primitive has a capsule collider that extends out past the end of the cyclinder which can unintentionally envelope a gameobject you arrange on top of it.

To fix do one of the following: 1) move the clickable object out of the other object's collider, 2) remove/deactivate the other object's collider, or 3) change the other object's collider to a type mesh collider and ensure your clickable is no longer inside it

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 FranciscoFontes · Jul 13, 2014 at 01:32 AM

Hello, my solution is to create the observer pattern.

MouseManager will check the objects you want to know about mouse events or states of the mouse.

IMouseObserver is implemented to perform the specific behavior of each mouse event.

http://www.orunz.com/tuto/mouse_manager.unitypackage

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

Answer by hacky97 · Dec 22, 2014 at 02:01 PM

The solution to all: guys, if you have NO Update() handler the OnMouseDown() handler wo't be fired.

Solution: Just add an empty oid Update(){} function

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 Eric5h5 · Dec 22, 2014 at 06:30 PM 0
Share

That's incorrect. There's no need for any Update function. On$$anonymous$$ouseDown works fine without it.

avatar image hacky97 · Feb 15, 2015 at 09:00 PM 0
Share

Sorry guys, I added an update handler, restarted the scene and assumed that the fix was the added Update handler. We encountered this problem many times in different Unity versions now and all that fixes it is restarting Unity or re-opening the scene.

avatar image
0

Answer by Deekor · Feb 15, 2015 at 04:38 AM

The function name is OnMouseDown and not onMouseDown - this was my problem. Thanks to zerokcm in comments above for pointing this out.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

OnMouseDown with 360 controller unity? 1 Answer

GameObject Array 1 Answer

Increase mouseclick area size for OnMouseDown() - For Mouse Shooting Game (Open to more ideas) 2 Answers

Is there something like OnMouseDown which doesn't require a collider on my prefab? 3 Answers

How to apply OnMouseDown on sprites according to z position... 3 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