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 Perrorist · Oct 10, 2021 at 05:40 AM · buttons

How do I find button number?

I need the number for a second button, which should be 1: Input.GetMouseButtonDown(1). However, it doesn't reckon that that's its number. If I supply 0, it executes the other button, as expected. I've tried 2 and 3, with no luck. How do I find the correct number?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by SmoothieTrash · Oct 10, 2021 at 09:18 AM

Hi @Perrorist You can use the new Input System's listen feature, to find the button's name, then map it to its corresponding number using the KeyCode docs:

Mouse0 The Left (or primary) mouse button.
Mouse1 Right mouse button (or secondary mouse button).
Mouse2 Middle mouse button (or third button).
Mouse3 Additional (fourth) mouse button.
Mouse4 Additional (fifth) mouse button.
Mouse5 Additional (or sixth) mouse button.
Mouse6 Additional (or seventh) mouse button.

for example: middleButton is Mouse2


Or you can write a simple script which prints which mouse button is pressed:

 using UnityEngine;
 using System.Collections;
 
 // Detects clicks from the mouse and prints a message
 // depending on the click detected.
 
 public class ExampleClass : MonoBehaviour
 {
     void Update()
     {
         if (Input.GetMouseButton(0))
         {
             Debug.Log("Pressed left btn.");
         }
 
         if (Input.GetMouseButton(1))
         {
             Debug.Log("Pressed right btn.");
         }
 
         if (Input.GetMouseButton(2))
         {
             Debug.Log("Pressed middle btn.");
         }
 
         if (Input.GetMouseButton(3))
         {
             Debug.Log("Pressed fourth btn.");
         }
 
         if (Input.GetMouseButton(4))
         {
             Debug.Log("Pressed fifth btn.");
         }
 
         if (Input.GetMouseButton(5))
         {
             Debug.Log("Pressed sixth btn.");
         }
 
         if (Input.GetMouseButton(6))
         {
             Debug.Log("Pressed seventh btn.");
         }
     }
 }


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
0

Answer by Perrorist · Oct 10, 2021 at 09:11 AM

Although I'm still curious as to the answer, I've resolved the reason for my question. (A button is not a button if it's an image.)

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 Bunny83 · Oct 10, 2021 at 11:23 AM 1
Share

Have you read the method you used carefully? It says: GetMouseButtonDown. So this method only checks if the mouse button has been pressed. This has absolutely nothing to do with any UI button you may have placed in your scene. Input.GetMouseButtonDown tells you if a certain mouse button has been pressed this frame. As you can read in the documentation (which you hopefully read before you used an unknown method) the index refers to the button number of your mouse. "0" is usually the left mouse button while "1" is usually the right mouse button. Since we now often have mice with more than 2 buttons this index has an open end, though not all mouse buttons have to be supported by the OS or Unity. Having a crazy gamer mouse with 10 buttons, it's not necessarily the case that you can use index 0 to 9 in that case. Those often come with propritary software to either emulate a game controller or to config what those mouse buttons do.


Since your question is vague what you mean by "button", how can we answer your question? Unity has 3 UI systems which are still actively used. Each of those system has the concept of an UI button. So you may should figure out first, what you're actually using. My guess would be that you use uGUI and a button from the UnityEngine.UI namespace. In most modern UI systems you generally do not wait or check for button presses in code. Instead you link an event to the button itself in the inspector. So a method that is executed when the button is pressed.

avatar image Perrorist Bunny83 · Oct 10, 2021 at 06:56 PM 0
Share

Thank you both, SmoothieTrash and Bunny83. I was indeed mistaking 'button' as the UI button instead of the mouse button. That explains why only one behaviour occurred despite which screen button I clicked on. I'll link an event instead.

Thanks again for your help.

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

128 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

Related Questions

What is the best way to handle a scenario game with a LOT of panels? 0 Answers

Some UI buttons suddenly don't work anymore 0 Answers

Pause Menu Buttons Not Working on First Attempt 2 Answers

How do I remove action listeners from a button without actually clicking the button? 0 Answers

Move Dropdown using arrow buttons 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