- Home /
GUI Button not highlighting correctly
I am currently having some difficulty with a button at the moment. I am running the Select() function to highlight the first button of my pause menu (Resume) to allow for arrow keys to be used for navigation in the GUI. This works fine the first time I pause the game. However if I leave the pause menu with the Resume button highlighted the script does not highlight the button the next time I open the pause menu. If I leave the pause menu without the resume button being highlighted the button highlights correctly.
Code for the selection and menu is below:
// Menu.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Menu : MonoBehaviour {
public Canvas startMenu; // Start menu.
public Canvas optionsMenu; // Options menu.
public Canvas exitMenu; // Exit menu.
public Canvas pauseMenu; // Pause menu.
public Canvas gameOverMenu; // Game over menu.
public Canvas restartMenu; // Restart menu.
public MenuStartSelected firstSelect; // Selects the starting element of the GUI to
// allow keyboard input.
public GameController gameController; // The game controller.
// Use this for initialization
// Gets the canvas components and sets startMenu as enabled
// and the rest as disabled.
void Start ()
{
// Getting canvas components.
startMenu = startMenu.GetComponent<Canvas>();
optionsMenu = optionsMenu.GetComponent<Canvas>();
exitMenu = exitMenu.GetComponent<Canvas>();
pauseMenu = pauseMenu.GetComponent<Canvas>();
gameOverMenu = gameOverMenu.GetComponent<Canvas>();
restartMenu = restartMenu.GetComponent<Canvas>();
startMenu.enabled = true;
optionsMenu.enabled = false;
exitMenu.enabled = false;
pauseMenu.enabled = false;
gameOverMenu.enabled = false;
restartMenu.enabled = false;
}
// Enables the start menu and disables the rest.
public void EnableStartMenu()
{
EnableMenu();
startMenu.enabled = true;
optionsMenu.enabled = false;
exitMenu.enabled = false;
pauseMenu.enabled = false;
gameOverMenu.enabled = false;
restartMenu.enabled = false;
firstSelect.SelectFirstStart();
}
// Enables the options menu and disables the rest.
public void EnableOptionsMenu()
{
startMenu.enabled = false;
optionsMenu.enabled = true;
exitMenu.enabled = false;
pauseMenu.enabled = false;
gameOverMenu.enabled = false;
restartMenu.enabled = false;
firstSelect.SelectFirstOptions();
}
// Enables the exit menu and disables the rest.
public void EnableExitMenu()
{
startMenu.enabled = false;
optionsMenu.enabled = false;
exitMenu.enabled = true;
pauseMenu.enabled = false;
gameOverMenu.enabled = false;
restartMenu.enabled = false;
firstSelect.SelectFirstExit();
}
// Enables the pause menu and disables the rest.
public void EnablePauseMenu()
{
EnableMenu();
startMenu.enabled = false;
optionsMenu.enabled = false;
exitMenu.enabled = false;
pauseMenu.enabled = true;
gameOverMenu.enabled = false;
restartMenu.enabled = false;
firstSelect.SelectFirstPause();
}
// Enables the game over menu and disables the rest.
public void EnableGameOverMenu()
{
startMenu.enabled = false;
optionsMenu.enabled = false;
exitMenu.enabled = false;
pauseMenu.enabled = false;
gameOverMenu.enabled = true;
restartMenu.enabled = false;
firstSelect.SelectFirstGameOver();
}
// Enables the restart menu and disables the rest.
public void EnableRestartMenu()
{
startMenu.enabled = false;
optionsMenu.enabled = false;
exitMenu.enabled = false;
pauseMenu.enabled = false;
gameOverMenu.enabled = false;
restartMenu.enabled = true;
firstSelect.SelectFirstRestart();
}
// Disables all menus. **Note: SetActive() needs to be used to avoid a bug.**
public void DisableMenu()
{
startMenu.gameObject.SetActive(false);
optionsMenu.gameObject.SetActive(false);
exitMenu.gameObject.SetActive(false);
pauseMenu.gameObject.SetActive(false);
gameOverMenu.gameObject.SetActive(false);
restartMenu.gameObject.SetActive(false);
}
// Enables all menus **Note: SetActive() needs to be used to avoid a bug.**
public void EnableMenu()
{
startMenu.gameObject.SetActive(true);
optionsMenu.gameObject.SetActive(true);
exitMenu.gameObject.SetActive(true);
pauseMenu.gameObject.SetActive(true);
gameOverMenu.gameObject.SetActive(true);
restartMenu.gameObject.SetActive(true);
}
// Returns to the main menu.
public void ReturnToRootMenu()
{
// Checks if the game is over and if it isn't the game
// is brought to the pause menu if it is then it is brought
// to the start menu.
if(GameController.score != 0)
{
EnablePauseMenu();
}
else
{
EnableStartMenu();
}
}
// Unpauses the game
public void StartGame()
{
DisableMenu();
GameController.UnpauseGame();
}
// Restarts the game.
public void RestartGame()
{
DisableMenu();
gameController.ResetGame();
}
// Sets the location audio assist as active.
public void EnableHorizontalNoise()
{
GameController.SetPlayerAudioAssist(true);
}
// Sets the location audio assist as inactive.
public void DisableHorizontalNoise()
{
GameController.SetPlayerAudioAssist(false);
}
// Sets the obstacle audio assist as active.
public void EnableObstacleAudio()
{
GameController.SetBlockerAudioAssist(true);
}
// Sets the obstacle audio assist as inactive.
public void DisableObstacleAudio()
{
GameController.SetBlockerAudioAssist(false);
}
// Sets the obstacle audio assist as inactive.
public void EnableMusic()
{
GameController.SetGameMusic(true);
}
// Sets the obstacle audio assist as inactive.
public void DisableMusic()
{
GameController.SetGameMusic(false);
}
// Exits the game.
public void ExitGame()
{
Application.Quit();
}
}
// MenuStartSelected.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class MenuStartSelected : MonoBehaviour
{
public Button startMenu;
public Button options;
public Button exit;
public Button pause;
public Button gameOver;
public Button restart;
// Selects the first button in the start menu.
public void SelectFirstStart()
{
startMenu.Select();
}
// Selects the first button in the options menu.
public void SelectFirstOptions()
{
options.Select();
}
// Selects the first button in the exit menu.
public void SelectFirstExit()
{
exit.Select();
}
// Selects the first button in the pause menu.
public void SelectFirstPause()
{
pause.Select();
}
// Selects the first button in the game over menu.
public void SelectFirstGameOver()
{
gameOver.Select();
}
// Selects the first button in the restart menu.
public void SelectFirstRestart()
{
restart.Select();
}
}
How do I get the button to highlight again in pause? I can't find an error anywhere in the code.
Answer by qwinzeth · Sep 24, 2018 at 05:27 AM
I had the same problem and managed to solve it by first setting the current selected button to null:
var defaultButton = GameObject.Find("PauseContinueButton");
EventSystem.current.SetSelectedGameObject(null);
EventSystem.current.SetSelectedGameObject(defaultButton);
Not all heroes were capes. You, sir, have fixed my problem.