Selectable/Button not being selected
Hi there!
I'm trying to get a button/selectable to be automatically selected upon being enabled (as my project is designed to be played with a gamepad/keyboard, I need to select the first button when displaying menus, etc.
I think the button is getting selected, it's just that the usual transitions for the selectable isn't working. I can still navigate around the menu as if the button was selected, it's just now showing as selected on screen. I've tried two different ways of implementing this system to try and get around the bug:
Using the event system:
private void OnEnable()
{
EventSystem.current.SetSelectedGameObject(null);
EventSystem.current.SetSelectedGameObject(transform.GetChild(0).gameObject);
}
And using GetComponent.
Button button;
int childIndex;
void OnEnable()
{
button = GetComponent<Button>();
childIndex = transform.GetSiblingIndex();
}
void Start()
{
if (childIndex == 0)
{
button.Select();
}
}
Using GetComponent will work only the first time the button is enabled, then when it is disabled and reenabled, the transition doesn't work and we end up with the same problem as before.
Does anybody have any experience about this? A quick search has shown people with similar problems who managed to do a quick fix using coroutines to set buttons a frame after, but I'd like to see if there are other ways around it.
Thanks!