- Home /
Navigating UI Buttons with Arrow Keys not working
I'm working on setting up a pause panel and a game over panel on my game. The pause menu is fine, and I can navigate between the 2 buttons (unpause and quit) easily with the arrow keys. However, the game over menu isn't working at all. Using EventSystems.current.SetSelectedGameObject(), the correct button is selected first, but when I press the arrow keys nothing happens. The selection is in the right spot but just can't move for some reason.
The visualized navigation arrows are correct and both panels have explicit navigation instructions, and in my troubleshooting I've tried making a new Canvas, making multiple new panels, and just duplicating the Pause panel and none of them work.
Below is the script that accesses the buttons and the panels, I've edited it so just the relevant stuff is on here bc my UIManager script is like 140 lines lol, so I can clarify if anything is confusing.
public GameObject pausedPanel;
public GameObject firstPausedButton;
public GameObject gameOverPanel;
public GameObject firstGameOverButton;
public bool gameOver;
private void Start()
{
pausedPanel.SetActive(false);
gameOverPanel.SetActive(false);
EventSystem.current.SetSelectedGameObject(null);
gameOver = false;
}
public void Pause()
{
if (pausedPanel.activeInHierarchy)
{
//unpause
pausedPanel.SetActive(false);
EventSystem.current.SetSelectedGameObject(null);
}
else
{
//pause
pausedPanel.SetActive(true);
EventSystem.current.SetSelectedGameObject(null);
EventSystem.current.SetSelectedGameObject(firstPausedButton);
}
}
public void GameOver()
{
gameOverPanel.SetActive(true);
EventSystem.current.SetSelectedGameObject(null);
EventSystem.current.SetSelectedGameObject(firstGameOverButton);
gameOver = true;
}
private void Update()
{
if(numErrors == 10)
{
GameOver();
}
}
Thanks so much for any help :-)
Answer by madelineedupre · Feb 21, 2021 at 09:56 PM
UPDATE: It wasn't working because the GameOver function was called in Update, so I think it was continuously making the selected null and then the first button, making it impossible to navigate. Anyway, it's working now that I moved the call to GameOver to a different function :-)