• 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 ammar26 · Aug 29, 2013 at 12:13 PM · inputmousemouseclicktwice

Mouse click is detected twice

I'm trying to detect two different mouse click but only a single mouse click is detected twice, I don't know whats wrong with the code but I can't detect two separate mouse clicks

 bool click = true;
 void Update()
 {
         if(click)
         {
                  if(Input.GetMouseButton(0))
                 {
                    print ("Click1");
                    click = false;
                 }
         }
         else
         {
                 if(Input.GetMouseButton(0))
                 {
                    print ("Click2");
                 }
         }
 }

On single mouse click, I got "Click2", it should be giving "Click1", can anyone explain why I'm getting "Click2" on first mouse click

Edit: copy paste, "update()" function mistake corrected to "Update()"

Comment
Add comment
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 Reply

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

Answer by robertbu · Aug 29, 2013 at 12:15 PM

'update()' with a lower case 'u' will not work. As for your issue, Input.GetMouseButton(), returns true for every frame the button is held down. It can easily return true for multiple frames. You want to use Input.GetMouseButtonDown() or InputGetMouseButtonUp(). Here is a bit of a rewrite to your logic:

 using UnityEngine;
 using System.Collections;
  
 public class Bug27 : MonoBehaviour {
     bool click = true;
     void Update()
     {
         if(click)
         {
                 if(Input.GetMouseButtonDown(0))
                 {
                   Debug.Log ("Click1");
                   click = false;
                 }
         }
         else
         {
                if(Input.GetMouseButtonDown(0))
                {
                 Debug.Log ("Click2");
                     click = true;
                }
         }
     }
 }
Comment
Add comment · Show 7 · 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 ammar26 · Aug 29, 2013 at 12:16 PM 0
Share

Sorry, it was a copy paste mistake .. $$anonymous$$y code has Update with Capital "U", and still output is same

avatar image robertbu · Aug 29, 2013 at 12:33 PM 0
Share

Did you test the code I pasted above using Get$$anonymous$$ouseButtonDown()? I tested it before I pasted it, and it worked fine. Note your original code was missing the 'click = true;' on line 21.

avatar image Polymo · Aug 29, 2013 at 12:41 PM 0
Share

Did you change the click variable from public to private later on? That once was the problem for me as Unity seems to keep the previously inspector set value in cache until scene reload or variable rena$$anonymous$$g.

avatar image ammar26 · Aug 29, 2013 at 12:42 PM 0
Share

No i didn't changed protection of variable

avatar image robertbu · Aug 29, 2013 at 12:42 PM 0
Share

On your original code, you are getting 'Click2' because your are using Get$$anonymous$$ouseButton(). Get$$anonymous$$ouseButton() returns true for every frame the mouse buttons is held down. A typical mouse click will last for several frames. If you replace your 'Input.Get$$anonymous$$ouseButton()' calls with 'Input.Get$$anonymous$$ouseButtonDown()' calls, you will get 'Click1' the first time the button is pressed. After that, you will get 'Click2' for all other mouse clicks.

Show more comments

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

18 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

Related Questions

Disable mouse input and cursor in game 2 Answers

Unity Web Player does not respond to mouse clicks 1 Answer

EventSystem - detect any click NOT on object 1 Answer

Map mouse click state to a Joystick button? 0 Answers

Unity sometimes misses Mouse Button Release 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