Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tohaveaname · Apr 06, 2016 at 11:15 AM · guibuttonhighlight

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LexGear · Nov 25, 2018 at 06:49 AM 0
Share

Not all heroes were capes. You, sir, have fixed my problem.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to have multiple sets of button highlights? 0 Answers

Is the 'highlighted state' the same as the 'selected state'? 2 Answers

Controlling GUI with Xbox 360 Controller 1 Answer

Resolutions 1 Answer

Generating tooltips for multiple buttons. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges