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:
- I checked if there are any error or warning messages, and nothing prints.
- I checked that CurrentUI and FirstSelectedObject are indeed the button I want to select.
- I tried calling this function one time at in Start(), and one time in Update(), and one time in both.
- I tried using only input.select(), and I tried using only EventSystem.SetSelected
- 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.