- Home /
Detect Right Click on GUI Buttons
Is it possible to easily detect weather the player has left or right clicked an OnGUI function Button.
I Know that placing a simple IF before the button will supply a true false for if it is left clicked, however how does one do this for right clicks.
Thanks guys, Dom
this method doesnt work anymore..... im using unity 5... not using the new gui but still using the old... i want to detect if im right clicking the gui.button etc :/
Answer by MeditatingGamer · Jul 03, 2013 at 09:48 PM
if(GUI.Button(new rect(0,0,0,0), "Button")) {
if(Input.GetMouseButtonUp(0)) {
Debug.Log("left click");
}
else if(Input.GetMouseButtonUp(1)) {
Debug.Log("right click");
}
}
I noticed your post, hope your still checking up on it. This is something I tossed together that works for me. And I also hope this helps you and whomever is coming across this problem.
Looks as of Unity 5.6 this isn't working anymore. @robertbu has the correct answer with using var e = Event.current;
.
Answer by robertbu · Jul 04, 2013 at 01:13 AM
You can look at the Event. Event.button is an int that indicates which button was pressed.
#pragma strict
function OnGUI() {
var e = Event.current;
if (e.type == EventType.MouseUp && e.button == 1)
Debug.Log("Right mouse button lifted");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613090823im_/https://answers.unity.com/themes/thub/images/avi.jpg)