- Home /
Why can't I use the other buttons in the UI menu?
Hi everyone! I am having an hard time understanding how to properly set the UI buttons of a Pause menu for use with a Joystick. My goal is simply to:
Stop the game and enter the pause menu by clicking the 'start' key on the joystick;
Navigate through the menu with the joystick;
Trigger the proper function associated to each UI button with another joystick key, say 'A';
Resuming the game either by pressing again on the 'start' key or by pressing on the 'Resume' button with another joystick key, say 'A' (just like it is done in almost any game supporting joysticks)
So far, I managed to do point 1 and 2, however, when it comes to the rest I encounter the following problems:
Only the first function within the Update() method is called, regardless of the button I am pressing with the 'A' key,
The other buttons work but only if I press the 'start' key,
Here below is my current script:
public class PauseMenu : MonoBehaviour
{
private bool isPaused = false;
public GameObject pausePanel;
public GameObject inventoryPanel;
public Button resumeButton, mainMenuButton, inventoryButton, SaveButton;
public GameObject player;
public bool usingPausePanel;
public string scene2Load;
void Start()
{
//Set-up pause and inventory menus to inactive as game starts
isPaused = false;
pausePanel.SetActive(false);
inventoryPanel.SetActive(false);
usingPausePanel = false;
//Add listeners to button click events
resumeButton.onClick.AddListener(Resume);
mainMenuButton.onClick.AddListener(Quit2Main);
inventoryButton.onClick.AddListener(SwitchPanels);
}
void Update()
{
if (Input.GetButtonDown("Submit"))
{
ResumeStartButton();
}
else if (isPaused)
{
if (Input.GetButtonDown("MenuSelect_A") && resumeButton.onClick != null)
{
Debug.Log("Resume");
Resume();
resumeButton.onClick.RemoveAllListeners();
}
else if (Input.GetButtonDown("MenuSelect_A") && mainMenuButton.onClick != null)
{
Debug.Log("Q2M");
Quit2Main();
}
else if (Input.GetButtonDown("MenuSelect_A") && inventoryButton.onClick != null)
{
Debug.Log("Inventory");
SwitchPanels();
}
}
}
public void Resume()
{
if (isPaused)
{
pausePanel.SetActive(false);
Time.timeScale = 1f;
usingPausePanel = false;
player.SetActive(true);
Debug.Log("In resume");
}
}
public void ResumeStartButton()
{
isPaused = !isPaused;
if (isPaused)
{
pausePanel.SetActive(true);
Time.timeScale = 0f;
usingPausePanel = true;
player.SetActive(false);
Debug.Log("In resumeStart");
}
else
{
pausePanel.SetActive(false);
Debug.Log("Resume game with start button");
Time.timeScale = 1f;
usingPausePanel = false;
player.SetActive(true);
}
}
public void Quit2Main()
{
SceneManager.LoadScene(scene2Load);
Time.timeScale = 1f;
Debug.Log("In Q2M");
}
First question:
I guess that the reason why only the first function that i want to call with the 'A' key (i.e. Resume() in my case) gets called is because whenever i press the A key the if statement will always result true, since in the Start() method I added a listener associated to each button. How can i detect that a specific button has been clicked and not another one? I was looking for something like xxxButton.onClick.IsInvoking() but that does not seem to exist;
Why do the button works with the 'Start' key instead?
Thanks in advance for the help!
Answer by Proliecan · Jun 23, 2021 at 10:00 AM
Here is a great Tutorial by Thomas Brush regarding exactly your goal:
Thanks for the link! However I saw this tutorial already and it does not talk much about handling the inputs. For what it's worth, my menu has the same functionalities as his, but it only works with the 'start' key i.e. the one i use to activate the menu. Once in the menu, I would prefer to use another key e.g. 'A' on the joystick.