Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
0
Question by dwathen · Mar 25, 2021 at 01:54 AM · errormouseclick

Left mouse button registers multiple clicks

Scenario: I want to be able to click on a card if it's not selected (and it is allowed to be selected) and move it up. Likewise, I would like to be able to click on a card if it is selected to move it down.

Issue: When I have the code to move the card up, given that it is allowed to be selected and that it is not currently selected, it works with no issues. It moves up as I expect and registers a single mouse click. However, when I add code to move the card back down if it is allowed to be selected and is currently selected, the card doesn't move. A little bit of debugging later, it looks like it's because it registers maybe two dozen mouse clicks, even though I'm using GetMouseButtonDown and clicking a single time.

I'm not sure what I'm doing wrong, but if someone could help, that would be awesome!

Code:

 void Update()
 {
     GetMouseClick();
 }

 void GetMouseClick()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Vector3 mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -10));

         RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

         if (hit)
         {
             if (hit.collider.CompareTag("Card"))
             {
                 ClickCard(hit.collider.gameObject);
             }
         }
     }
 }

 void ClickCard(GameObject card)
 {
     Selectable selectable = card.GetComponent<Selectable>();
     if (selectable.CanSelect && !selectable.IsSelected)
     {
         card.transform.Translate(0, 0.75f, 0);
         selectable.IsSelected = true;
         print("Clicked from Not Selected.");
     }
     else if (selectable.CanSelect && selectable.IsSelected)
     {
         card.transform.Translate(0, -0.75f, 0);
         selectable.IsSelected = false;
         print("Clicked from Selected.");
     }
 }

Debug window:

alt text

buttonclickissue.png (58.2 kB)
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 MikeNewall · Mar 25, 2021 at 09:31 AM 1
Share

Do you have more than one instance of the script that checks the mouse input in your scene?

You can check easily if you click on each log in the console. It should highlight the object responsible for that log in the inspector. Make sure they are not co$$anonymous$$g from different objects.

avatar image Smurfj3 · Mar 25, 2021 at 10:29 AM 1
Share

Nothing seems to be wrong with the code however to find out if the GetMouseClick() method or ClickCard() method is called anywhere else, just check if there is any other references to them, I know in VS you can simply right click on a method and select "Find all references". Also like the other comment mentioned, make sure you don't have it instantiated multiple times.

1 Reply

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

Answer by dwathen · Mar 26, 2021 at 03:16 AM

So, I was able to find the problem. As @MikeNewall and @smurfj3 mentioned, I had the script attached to all of the cards, which means it fired once for each script.

Thanks for your help!

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

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

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

Resolution Dialog: Availabe Resolutions when enabling "windowed" 0 Answers

WARNING : Assignment to temporary. 1 Answer

Unity just showing blank screen 0 Answers

when trying to run a unity game I often get a fatal error, why? 1 Answer

my mouse freezes when I hold down any key. 0 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