Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

First Selected not working when activating buttons

Discussion in 'Input System' started by juicedup, Dec 6, 2020.

  1. juicedup

    juicedup

    Joined:
    Aug 18, 2019
    Posts:
    47
    I have a button gameobject in the scene that pops up a UI when it's collided with.
    The problem is when using a controller the new input system wont let me switch between buttons.



    I have a property in a script attached to the UI object that has a variable to the event system that assigns the left button as the first selected game object when the item is collided with, but that didn't help.



    Currently I have to press the submit before I can chose between UI buttons.
    Any help would be greatly appreciated!!!!
     
    victorgeavilla likes this.
  2. juicedup

    juicedup

    Joined:
    Aug 18, 2019
    Posts:
    47
    had to use setselected on the event system :(
     
    victorgeavilla likes this.
  3. victorgeavilla

    victorgeavilla

    Joined:
    Dec 18, 2021
    Posts:
    1
    Had the same problem, Thank you!
     
  4. angelonit

    angelonit

    Joined:
    Mar 5, 2013
    Posts:
    40
    Thanks for the hint, Here's the script I use (just put it on the canvas that's parent to all the selectables (buttons etc) in the UI)
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class SelectionUI : MonoBehaviour
    7. {
    8.     [Header("_____ Visible For Debugging _____")]
    9.     public EventSystem EventSystem;
    10.     public List<Selectable> Selectables = new List<Selectable>();
    11.  
    12.     private void Start()
    13.     {
    14.         EventSystem = FindObjectOfType<EventSystem>(true);
    15.         GetChildSelectables();
    16.     }
    17.     private void Update()
    18.     {
    19.         if (EventSystem.currentSelectedGameObject != null && EventSystem.currentSelectedGameObject.activeInHierarchy)
    20.         {
    21.             return;
    22.         }
    23.         else
    24.         {
    25.             foreach (var item in Selectables)
    26.             {
    27.                 if (item.isActiveAndEnabled)
    28.                 {
    29.                     EventSystem.SetSelectedGameObject(item.gameObject);
    30.                     Debug.Log("Selected: " + item.name);
    31.                     return;
    32.                 }
    33.             }
    34.         }
    35.     }
    36.     [ContextMenu("GetChildSelectables")]
    37.     public void GetChildSelectables()
    38.     {
    39.         Selectables.Clear();
    40.         Selectables.AddRange(gameObject.GetComponentsInChildren<Selectable>(true));
    41.     }
    42. }