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

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

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.

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