Strange Unity 3D Bugs #1: Select()
One problem I encountered while making Scrap Galaxys new GUI was the following. Since Scrap Galaxy is a game where the main input device is a gamepad the main menu must work with gamepads. In Unity 3D's EventSystem that handles navigation you need to have a button focused, selected, in order for navigation to work. It is not possible to select a button if nothing is already selected.
Problem
I had problem with the Select() function. This following script is attached to a root component in the menu dialog. The menu dialog is enabled or disabled depending on menu screen. When it enables it does select the desired button all the time, specially not when starting in the editor.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class SelectWhenEnabled : MonoBehaviour {
public Selectable selectable;
IEnumerator OnEnableCoroutine() {
yield return new WaitForSeconds(0.1f);
selectable.Select();
}
void OnEnable() {
StartCoroutine(OnEnableCoroutine());
}
}
- http://pastebin.com/DNffn2SG
The odd thing is that the button is somewhat selected, you can use it to navigate to other buttons and click on it via the gamepad. The animator is however in “normal” mode, and not in highlighted mode.
Solution
One should call OnSelect on the button. Following script works all the time for me.
using UnityEngine;
using UnityEngine.UI;
public class SelectWhenEnabled : MonoBehaviour {
public Selectable selectable;
void OnEnable() {
selectable.Select();
selectable.OnSelect(null);
}
}
- http://pastebin.com/FVYEAnMy












