Search Unity

Submenus and FirstSelected issue

Discussion in 'UI Toolkit' started by jakebaker865, Jun 7, 2020.

  1. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36
    Hoping someone can help with this. I have a menu where you can select submenus (Pause the game, navigate down to a volume button, select, takes you to submenu for volume slider, navigate to "back" button, select, and are taken back to main menu.

    The issue comes when I go "back" from the volume submenu. It is supposed to select the first button in the scene so I can continue navigating with the arrow buttons (or gamepad) but nothing shows as selected so the arrow keys don't work.

    The code below is on the pause menu, and the volume submenu has a script with just the couroutine so that the first selected is the slider.

    Code (CSharp):
    1. public class PauseMenu : MonoBehaviour
    2. {
    3.  
    4.     public static bool GameIsPaused = false;
    5.  
    6.     public GameObject pauseMenuUI;
    7.     private GameObject player;
    8.     public PlayerController controllerScript;
    9.     public Button btn;
    10.     public EventSystem es;
    11.  
    12.     void Awake()
    13.     {
    14.         player = GameObject.FindWithTag("Player");
    15.        
    16.        
    17.     }
    18.  
    19.     void OnEnable()
    20.     {
    21.         StartCoroutine(SelectMyButton());
    22.     }
    23.  
    24.     void OnDisable()
    25.     {
    26.         StopCoroutine(SelectMyButton());
    27.     }
    28.  
    29.     IEnumerator SelectMyButton()
    30.     {
    31.         yield return null; // Wait 1 frame for UI to recalculate.
    32.         if (es.currentSelectedGameObject != null)
    33.         {
    34.             var previous = es.currentSelectedGameObject.GetComponent<Selectable>();
    35.             if (previous != null)
    36.             {
    37.                 previous.OnDeselect(null);
    38.                 es.SetSelectedGameObject(null);
    39.             }
    40.         }
    41.         es.SetSelectedGameObject(btn.gameObject);
    42.         btn.OnSelect(null);
    43.     }
    44.  
    45.     //for Debug Purposes
    46.     /*void OnGUI()
    47.     {
    48.         // You can also inspect the EventSystem to see the current selection.
    49.         GUILayout.Label("Selection=" + es.currentSelectedGameObject);
    50.     }*/
    51.  
    52.  
    53.  
    54.  
    55.  
    56. // Update is called once per frame
    57. void Update()
    58.     {
    59.         if (Input.GetButtonDown("Cancel"))
    60.         {
    61.             if (GameIsPaused)
    62.             {
    63.                 Resume();
    64.             }
    65.             else
    66.             {
    67.              
    68.                 Pause();
    69.             }
    70.         }
    71.     }
    72.  
    73.     public void Resume()
    74.     {
    75.         controllerScript.canMove = true;
    76.         pauseMenuUI.SetActive(false);
    77.         Time.timeScale = 1f;
    78.         GameIsPaused = false;
    79.     }
    80.  
    81.     void Pause()
    82.     {
    83.  
    84.         StartCoroutine(SelectMyButton());
    85.         controllerScript.canMove = false;
    86.         pauseMenuUI.SetActive(true);
    87.         Time.timeScale = 0f;
    88.         GameIsPaused = true;
    89.     }
    90.  
    91.     public void LoadMenu()
    92.     {
    93.         controllerScript.canMove = true;
    94.         Time.timeScale = 1f;
    95.         SceneManager.LoadScene(1);
    96.     }
    97.  
    98.     public void QuitGame()
    99.     {
    100.         Application.Quit();
    101.     }
    102.  
    103.  
    104.  
    105. }