- Home /
commands.close.hint.6
can i "click" a button by pressing a key or gamepad button?
im making a pause menu and i want to have each option labeled with the designated button next to it, but im not sure the script i could use to "click" an on screen button.
i've already tried just labeling the buttons and having the corresponding button do what i want it to but that just makes my pause menu close immediately.
~C#~
void Update() {
if (Input.GetButtonUp("A"))
Time.timeScale = 1.0f;
((Behaviour)player.GetComponent("MouseLook")).enabled = true;
((Behaviour)cam.GetComponent("MouseLook")).enabled = true;
((Behaviour)cam.GetComponent("ShowSkin")).enabled = false;
}
ShowSkin is the name of my script with the GUI... but if i add this, as soon as i open my pause menu it closes instantly.
2 ways to answer this question.
tell a way to "click" a button with script
help me fix my script here so it doesn't close immediately.
please help
Unrelated tip: use the generic GetComponent:
// C#
player.GetComponent<$$anonymous$$ouseLook>().enabled = true;
// JS
player.GetComponent.<$$anonymous$$ouseLook>().enabled = true;
It's more efficient and more readable since no need to cast.
Please go back and show us when you tried to do it in your OnGUI, because that was the correct approach, whereas what you have now looks like you are heading off in the wrong direction. Having a button shortcut a GUI button is pretty straightforward, so you must have made a small mistake back then (eg. put the GUI.Button after the Input.GetButtonUp in the if
predicate).
Answer by Molix · Aug 11, 2011 at 11:05 PM
You are missing braces around the body of your if statement, so only the first statement is conditional on pressing 'A'. i.e. you need:
void Update()
{
if (Input.GetButtonUp("A"))
{
Time.timeScale = 1.0f;
((Behaviour)player.GetComponent("MouseLook")).enabled = true;
((Behaviour)cam.GetComponent("MouseLook")).enabled = true;
((Behaviour)cam.GetComponent("ShowSkin")).enabled = false;
}
}
haha thanks... i need to take a break lol... now im having another problem, when i press A to resume i also Jump, is there any way to fix that? besides change the button?
the button to resume on the GUI is the button to Jump in the game .....
You could have your jump check to see if the timeScale is < 1 (in addition to checking the button).
Change either the jump or unpause button, or you'll need something to check the state.
Ex) bool paused = true; if (!paused) Jump()
else Unpause()