Why does the button get unhighlighted after repuasing?

Viewed 45

I'm facing a really weird issue with the pause menu. Whenever I pause and then unpause and pause again, the buttons lose their focus. The first time I pause:

enter image description here

Second time I pause:

enter image description here

Only the first time is working as expected, all pauses after that are like the second image(no button highlighted). I couldn't figure out the reason.

enter image description here

Here is my code:

 void Update()
    {
        var gamepad = Gamepad.current;
        var keyboard = Keyboard.current;
        if (gamepad == null && keyboard == null)
            return; // No gamepad connected.
        if ((gamepad != null && gamepad.startButton.wasPressedThisFrame) || (keyboard !=null && keyboard.pKey.wasPressedThisFrame))
        {
            if (GameIsPaused)
            {
                Resume();

            }
            else
            {
                Pause();
            }
        }
    }
    public void Resume()
    {
        player.gameObject.GetComponent<MyPlayerMovement>().enabled = true;
        pauseMenuUI.SetActive(false);
        Time.timeScale = 1f;
        GameIsPaused = false;
      //  GameObject.Find("ThePlayer").GetComponent<MyPlayerMovement>().enabled = false;
      //  GameObject.Find("ThePlayer").GetComponent<InteractIcons>().enabled = false;
    }

    private void Pause()
    {
        player.gameObject.GetComponent<MyPlayerMovement>().enabled = false;
        pauseMenuUI.SetActive(true);
        resumeBtn.Select();
        Time.timeScale = 0f;
        GameIsPaused = true;
     //   GameObject.Find("ThePlayer").GetComponent<MyPlayerMovement>().enabled = true;
      //  GameObject.Find("ThePlayer").GetComponent<InteractIcons>().enabled = true;

    }

I tried assigning the button in the inspector to a variable and then added this variable like so resumeBtn.Select(); in Pause() but it changed nothing.

enter image description here

enter image description here

enter image description here

enter image description here

Note that when the buttons are not highlighted, they get highlighted only after I press the arrows up or down or move the gamepad's analogue stick up or down. but at first, they are not highlighted. How can I fix this?

1 Answers

In your event system you specified first selected to you resume button, however that seems to be not enough.

I'm not really sure how it works (this paragraph may be absolute bs), but after empirical research I think that event system sets its current selected item to the first selected reference during its awake or start. On disable, it seems to clear its current selected, but doesn't set it again on its enable.

One way to tackle this problem is to manually set current selected item when bringing up pause menu: eventSystem.SetSelectedGameObject(resumeButtonGameObject); where eventSystem is obviousely of EventSystem type.

Related