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.

  1. tell a way to “click” a button with script
  2. help me fix my script here so it doesn’t close immediately.

please help

Unrelated tip: use the generic GetComponent: // C# player.GetComponent<MouseLook>().enabled = true; // JS player.GetComponent.<MouseLook>().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).

As you are not the first one to add modding to your game there are others who have already done this for you. Just check how others do this. Do not try to unnecessarily reinvent the wheel unless it is for educational purposes because you yourself want to learn how to do it and enjoy the process of searching for a solution.

1 Answer

1

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?

You need to not Jump if the GUI is open.

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).