- Home /
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:
- 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.
Answer by Zobotron · Oct 29, 2021 at 01:56 PM
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.
Your answer
 
 
             Follow this Question
Related Questions
NullReferenceException after passing InputField gameObject into EventSystem.SetSelectedGameObject() 1 Answer
UI Element Got & Lost Focus Handling 2 Answers
Button is not being clicked. 1 Answer
Can I Simulate the "Submit" Event with Gamepad Buttons? 0 Answers
Button.Select(); does not highlight? 12 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                