How do I tell which mouse button was used to click a gui button?

I have a GUI button that is supposed to do one thing when left clicked, and another thing when right clicked. Since

if (GUI.Button(Rect(0, 0, 20, 20), “hello world”))

returns true for any mouse button, I need a way of detecting which mouse button was pressed. I tried just using Input.GetMouseButton(), but

Input.GetMouseButton(0)

is always false on a GUI button. Is this even possible?

You can do something like the follwing…

1.) Go to Edit → Project Settings → Input

2.) Add an input type named ‘MouseButtonLeft’, set its ‘Positive Button’ to ‘mouse 0’

3.) Add an input type named ‘MouseButtonRight’, set its ‘Positive Button’ to ‘mouse 1’

4.) On your scene manager script… write something like the following…

string lastMouseButtonPressed = "";
	
    void Update () {
        if (Input.GetButtonDown("MouseButtonLeft"))
            lastMouseButtonPressed = "Left button";
        
        if (Input.GetButtonDown("MouseButtonRight"))
            lastMouseButtonPressed = "Right button";
	}

    void OnGUI()
    {
        GUI.Label(new Rect(0, 0, 100, 100), lastMouseButtonPressed);

        if (GUI.Button(new Rect(20, 20, 100, 100), "My Button") && lastMouseButtonPressed == "Left Button")
        // Do something with button when left button pressed on it

        if (GUI.Button(new Rect(20, 20, 100, 100), "My Button") && lastMouseButtonPressed == "Right Button")
        // Do something with button when right button pressed on it
    }

What are you asking? Are you asking the script to identify what button is pressed so the outcome is different (left click on button means level 1, right click means level 2). Or do you just want the user to click a specified button. For that, you need:

(Input.GetButtonDown("Fire 1"))

If so, change the “fire 1” to whatever specified button. If not, clear up what you’re trying to say.