- Home /
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?
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.");
}
}
}
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.)
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.
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

Follow this Question
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