• 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
-1
Question by Tycho_9000 · Nov 21, 2013 at 06:24 AM · mouseclick

Input.GetMouseButtonDown(0) is only detecting one click

So i'm trying out a tutorial on how to use unity, and I've run into a problem. I'm trying to use Input.GetMouseButtonDown(0) to detect when the mouse button has clicked on either blank space or an object, but no matter what it will only detect 2 clicks total (one for not hitting an object, and one for hitting one). Here is the code I've got:

 function Update()
 {
     //want to use the left mouse button to select on 
     //game objects in the scene
     if(Input.GetMouseButtonDown(0))
     {
         print("Yes the button works");
         var hit : RaycastHit;
         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, hit, 100.0))
         {
             print("You hit an object");
             
         }
     }
 }

Has the way that GetMouseButtonDown(0) works changed? or is there something i'm missing in my code here (also, the tutorial I'm following is here: http://vimeo.com/album/1512103/video/18906992 in case that helps)

Comment
Add comment · Show 2
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 Ross_B · Nov 21, 2013 at 07:14 AM 0
Share

So your output is: "Yes the button works" "Yes the button works" "You hit an object" No matter what? Or is it: "Yes the button works" "You hit an object"?

avatar image Huacanacha · Nov 21, 2013 at 07:16 AM 0
Share

If you want to check for the button being held down (rather than clicked once) use Input.$$anonymous$$ouseButton.

Input.$$anonymous$$ouseButtonDown only returns true in the same frame that the button was pressed.

2 Replies

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

Answer by Eric5h5 · Nov 21, 2013 at 07:13 AM

Turn off "collapse" in the console, or look at the number at the right which tells you how many times a particular message was generated.

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 Tycho_9000 · Nov 21, 2013 at 10:48 AM 0
Share

Thankyou! I didn't notice that I had collapse selected in the console, won't do that again

avatar image
0

Answer by Zshelley · Nov 21, 2013 at 07:32 AM

MouseButtonDown is only supposed to be true if the button has just changed from being 0 to being 1. IF you are looking for something to always return true if the mouse is down, look up the unity documentation on "Input" and find the appropriate function.

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 D3struct10n2806 · Dec 03, 2015 at 12:44 AM 0
Share

Get$$anonymous$$ouseButtonDown will only execute once, Get$$anonymous$$ouseButton will execute with each frame as long as the button is being held down.

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

23 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

Related Questions

How To Perform A Mouse Click On Game Object 5 Answers

Mouse controlled Laserpointer 1 Answer

OnMouseDown doesn't work 1 Answer

Clicking button locks mouse, doesn't activate button. how do I fix this? 0 Answers

Change button texture when its clicked 1 Answer


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