Selecting a selectable using script doesn't seem to work anymore?

Hello everyone,

I’m trying to write a system that automatically selects a “selectable” variable after a menu is instantiated. This way there’s always a button selected at the beginning.

Here’s my function that selects UI Elements:

public void SelectElement(Selectable input)
{
      Debug.Log("Selecting element: " + input.gameObject.name);
      input.Select();
      EventSystem.current.SetSelectedGameObject(input.gameObject);
}

And Here’s where I call it:

if (CurrentUI.FirstSelectedObject)
 {
     //TODO selecting object should be here
     Debug.Log("Selecting first object");
     SelectElement(CurrentUI.FirstSelectedObject);
}

And nothing seems to work. Even though both Debugging Messages print correctly and I checked all of the following:

  1. I checked if there are any error or warning messages, and nothing prints.
  2. I checked that CurrentUI and FirstSelectedObject are indeed the button I want to select.
  3. I tried calling this function one time at in Start(), and one time in Update(), and one time in both.
  4. I tried using only input.select(), and I tried using only EventSystem.SetSelected
  5. I have checked that there’s an event system that works normally. In fact navigation works perfectly once I click on a button using a mouse instead of the script.

This is bothering me because I have used this code before and It worked with that same system. Is this a bug? or did I forget something?

Update:

I also noticed something interesting. When I click on the event system it displays the following on the top:
“Selected:Button (UnityEngine.GameObject)”

So it clearly selects it, but the selected color doesn’t display, and I can’t click submit to press it (even though I normally can when I select manually and press submit after that). OnSelect also doesn’t get called.

I had the same problem and the solution is to keep the state of your selectable and update them in Update

public class GameLoaderCanvas : MonoBehaviour
{
    enum SelectedButton
    {
        Load,
        New,
        Quit,
        None
    }

    [SerializeField] Button loadButton;
    [SerializeField] Button newButton;
    [SerializeField] Button quitButton;

    SelectedButton selectedButton = SelectedButton.None;

    private void Start()
    {
        loadButton.Select();
        selectedButton = SelectedButton.Load;
    }

    private void Update()
    {
        switch (selectedButton)
        {
            case SelectedButton.Load:
                loadButton.Select();
                break;
            case SelectedButton.New:
                newButton.Select();
                break;
            case SelectedButton.Quit:
                quitButton.Select();
                break;
            case SelectedButton.None:
                break;
        }
    }

    void OnUp()
    {
        Debug.Log("Up");

        switch (selectedButton)
        {
            case SelectedButton.Load:
                selectedButton = SelectedButton.Quit;
                break;
            case SelectedButton.New:
                selectedButton = SelectedButton.Load;
                break;
            case SelectedButton.Quit:
                selectedButton = SelectedButton.New;
                break;
            case SelectedButton.None:
                selectedButton = SelectedButton.Load;
                break;
        }
    }

    void OnDown()
    {
        Debug.Log("Down");

        switch (selectedButton)
        {
            case SelectedButton.Load:
                selectedButton = SelectedButton.New;
                break;
            case SelectedButton.New:
                selectedButton = SelectedButton.Quit;
                break;
            case SelectedButton.Quit:
                selectedButton = SelectedButton.Load;
                break;
            case SelectedButton.None:
                selectedButton = SelectedButton.Load;
                break;
        }
     }
}

I hope this can help.