0
\$\begingroup\$

To support the use of a gamepad in the options menu I set SetSelectedGameObject and firstSelectedGameObject (which doesn't seem to do anything) to the button in the upper left, so it's highlighted. This works fine in my pause and option menus (panels that are enabled/disabled) but there's also a third panel that displays the controls as an image. On this screen the first button is set as selected but it doesn't get highlighted until I go back to the pause menu and then open the "controls" screen again.

The Hierarchy:

Hierarchy

The ControlsPanel looks like this: click

All 4 buttons are set to "automatic" and they're all accessible to the gamepad but, like I said, the first one ("Keyboard") doesn't light up the very first time I access the panel. It should be like this:

  • Click on "Controls" in the pause menu
  • ControlsPanel is enabled
  • "Keyboard" button is highlighted
  • Pressing right on the gamepad highlights the next button ("Controller")

What actually happens:

  • Click on "Controls" in the pause menu
  • ControlsPanel is enabled
  • "Keyboard" button is still grey
  • Pressing right on the gamepad highlights the next button ("Controller")
  • Press the "Back" button ("ControlsPanel" is disabled and "PausePanel" enabled)
  • Click on "Controls" again
  • This time the "Keyboard" button is highlighted

The "ControlsPanel" script:

public Image pic;
public GameObject controllerChoicePanel;
public GameObject firstControls;
private String controls;
private bool windows;

void Start() {
    controls = GetControls();
    windows = GetOS();
    pic.sprite = Resources.Load<Sprite>(controls);

    if(windows) {
        controllerChoicePanel.SetActive(true);
        EventSystem es = EventSystem.current;
        es.firstSelectedGameObject = firstControls;
        es.SetSelectedGameObject(firstControls);
    }
}

Any idea why it would work fine in my pause/options menu but not in the "Controls" one, even though I'm doing the same thing?

\$\endgroup\$
1
\$\begingroup\$

es.currentSelectedGameObject.name output "Keyboard", so the button was indeed set properly, it just didn't highlight it (no idea why). Resetting the selected GameObject before setting it again helped:

if(windows) {
    controllerChoicePanel.SetActive(true);
    EventSystem es = EventSystem.current;
    es.SetSelectedGameObject(null); //Resetting the currently selected GO
    es.firstSelectedGameObject = firstControls;
    es.SetSelectedGameObject(firstControls);
}

Important: Only do this at the very beginning if you're using a gamepad, if you reset it every time, you won't be able to navigate to the next button! If you want to support both gamepad and mouse navigation, you have to do this every time though, otherwise clicking on the button will keep it highlighted until you click on another one (if the buttons' navigation is set to "Automatic"). So in the end you'll have to check where the input is coming from (gamepad or mouse).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.