"SetSelectedGameobject" only highlights button on second try (Unity 2017.3)

by Neph   Last Updated October 11, 2018 13:13 PM

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?



Related Questions


Unity - C# - Overlapping Rectangles not separating

Updated March 26, 2015 07:26 AM


Unable to instantiate objects Straight in unity

Updated April 04, 2015 15:05 PM


How to find nearest gameobject on one side

Updated June 25, 2016 08:05 AM