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

Answer by V-Jankaitis · Jul 22, 2015 at 01:21 PM

If the collider you are aiming for is inside a child of another object with colider, it will always hit the parent collider (as fas as I observed)

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 plasticYoda · Jun 07, 2015 at 03:24 AM

My issue was the object with the collider was not in a layer that was being rendered by the camera... I wanted the collider on a mesh that I could 'turn' off when not debugging - broke my OnMouse stuff, so just disabled the mesh instead.

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 Prabhakaran · May 08, 2015 at 01:19 PM

Hi,

The same issue with me , even after doing the options given above i didnt get the event fired so changed the code to be something like this

void Update()

{

     if (Input.GetMouseButtonDown(0) && this.guiTexture.HitTest (Input.mousePosition) == true) 
 {
     mbMouseDown = true;
 }
 if (Input.GetMouseButtonUp (0))
     mbMouseDown = false;

}

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 pikkupr · May 06, 2015 at 07:17 PM

this tutorial uses OnMouseDown() function.. hope this will help

http://techmon.in/u3d/2playertictactoe/

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