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
6
Question by AndreasX12 · Jul 04, 2013 at 06:49 PM · buttonmouseclickupdown

OnMouseDown() Doesn't work

Hello, I usually never have problems with this, but somehow I can't get the OnMouseDown() to work. This is my code:

 using UnityEngine;
 using System.Collections;
 
 public class Menu : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     
     
      void OnMouseDown() {
         Debug.Log("Clicked");
     }
 }


I have tryed with other stuff instead of Debug.Log("Clicked"); but it doesn't detect the click at all.

I have a collider on my gameobject. The script is attached to the same gameobject as the collider.

I don't know what can be causing this, but that's why I ask here ;)

Thanks, Andreas :-)

Comment
Add comment · Show 1
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 AndreasX12 · Jul 04, 2013 at 07:34 PM 0
Share

Thank you sooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo much robertbu :D :D I forgot all about checking if anything else was in the way :) Please convert to answer, so I can "Accept" it :) :D

17 Replies

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

Answer by robertbu · Jul 04, 2013 at 07:26 PM

  • Make sure the script is attached to the game object you want to be clickable.

  • Make sure you have an enabled collider

  • Make sure you have not resized or changed the position of the collider

  • Make sure there are no other colliders in the way. OnMouseDown() works just like a Raycast()...the first thing hit is what gets the OnMouseDown().

  • If everything looks okay and it still does not work, delete the object and start over by adding the new object, adding the script...

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

Answer by Turbine · Apr 25, 2014 at 02:12 AM

Necro post for prosperity:

OnMouseDown works completely differently if there is a RigidBody somewhere in the hierarchy. It actually won't call OnMouse functions on the clicked object but it will instead call them on the RigidBody's game object instead!

In pseudo code:

 void UnityApiMouseEvents()
 {
 RaycastHit hit;
 if ( Physics.Raycast(hit))
 {
  if ( hit.rigidbody != null )
      hit.rigidbody.gameObject.SendMessage("OnMouseDown");
  else
      hit.collider.SendMessage("OnMouseDown");
 }
 
 }
Comment
Add comment · Show 6 · 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 Mikael-H · May 05, 2014 at 08:57 PM 0
Share

Thanks, you saved my day.. :)

avatar image Erethan · Jul 26, 2016 at 11:05 PM 0
Share

I've been looking for this for ages! Thanks! Why is that not mentioned on the Docs??

avatar image Melann · Sep 07, 2017 at 02:53 PM 0
Share

Yes, thank you for necro-posting. It is 2017 and the difference in behavior introduced by a rigidbody in the hierarchy is still not mentioned in the documentation.

avatar image vonSchlank · Jan 02, 2018 at 09:31 AM 1
Share

Thanks! Eternal life and free beer for you!

avatar image zingzingzingbah · Apr 16, 2018 at 09:42 AM 0
Share

Ahhh man...THAN$$anonymous$$ YOU... but why??

Show more comments
avatar image
14

Answer by NateJC · Dec 20, 2016 at 04:37 AM

For UGUI UI objects, instead of OnMouseDown, use OnPointerDown:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems; // Required when using Event data.
 
 public class ExampleClass : MonoBehaviour, IPointerDownHandler // required interface when using the OnPointerDown method.
 {
     public void OnPointerDown (PointerEventData eventData) 
     {
         Debug.Log (this.gameObject.name + " Was Clicked.");
     }
 }

Alternatively, this can also be handled in the Inspector by adding an Event Trigger component.

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 zaxdivine · Aug 20, 2018 at 03:53 PM 0
Share

Thanks! It worked.

avatar image Zeftax · Apr 18 at 06:55 PM 0
Share

This was the solution for me, thanks.

avatar image
4

Answer by anthodb · Jan 16, 2015 at 10:53 AM

Just to add my little part in this, I was having OnMouseDown not firing at all on my gameObject. Did try everything for about one hour to finally close Unity (4.5.5) and re-open. That did the trick !!

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 KevinCodes4Food · Mar 28, 2015 at 12:24 AM 0
Share

Thanks, anthodb, this just happened to me, and your recommendation to close and reopen Unity fixed it.

I am running Unity v4.6.0f3 on Windows 8.1. Wonder if this is a known bug...

avatar image idiot333 · Apr 18, 2015 at 08:44 PM 0
Share

Thanks bro that helped me too.

avatar image Black_Stormy · Aug 19, 2017 at 11:54 PM 0
Share

No idea why but this worked for me as well EDIT: worked once then stopped working forevermore.

avatar image
2

Answer by bakinto · May 20, 2020 at 03:58 AM

This may seems so stupid, but make sure you click on your game window and not the view!! that's what happened with me, and almost lost it before I realise im clicking on th view not the game widow :T lol

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

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

Related Questions

GUI Repeatbutton up? 1 Answer

Help with resize script 1 Answer

Problem with OnMouseDown 1 Answer

GUI Button OnMouseUp? 2 Answers

How to check if mouse over object without adding so many scripts? 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