Highlight button via script at runtime

okay so i have a menu canvas with UI buttons on them, i need to know is there anyway i can highlight one via script at runtime so i can scroll through them with the controller, much like when you have a first selected on the event system but i need a new selected because i am switching canvases. i cant seem to find the answer anywhere! i know this isnt the code but im looking for something like button.state.highlight = true; ? im using C#

Hi i Think You are looking For something Like This [Selectable.Select()][1]

`UnityEngine.UI.Button btn `;

`btn.Select();`

More details on UI Buttons can be found on [Unity Docs][2].

Hope it Helps.

HI, I know this question is a bit old but, if that may help, you can check my answer on an other topic EventSystem.SetSelectedGameObject doesn't highlight button - Unity Answers

@Woozybytes

When using this code, it states btn is null…
how do you find the actual Object to assign it to the btn?

Hi, any advice on how to deselect a button?

Hi there,

for those still looking to select a button first when a menu/canvas is enabled, and being able to navigate with a gamepad controller, here’s my solution:

using UnityEngine;
using UnityEngine.EventSystems;


public class QuickMenu : MonoBehaviour
{
    [SerializeField] 
    private GameObject quickMenu; //The menu/canvas you want to enable

    [SerializeField] 
    private GameObject firstButtonMenu; //allow you to drop in the inspector the button you want to higlight first in your menu/canvas


    void Update()
    {
        InputMenu();
    }

    public void InputMenu()
    {
        if (Input.GetKeyDown(KeyCode.JoystickButton4)) 
        {
            if (!quickMenu.activeInHierarchy) //check if the menu/canvas is not active in the hierarchy
            {
                quickMenu.gameObject.SetActive(true); //activate your menu

                EventSystem.current.SetSelectedGameObject(null); //clear any previous selection (best practice)
                EventSystem.current.SetSelectedGameObject(firstButtonMenu); //selection the button you dropped in the inspector
            }
            else
            {
                quickMenu.gameObject.SetActive(false);
            }
        }
    }
}
  • Check the unity doc for the joystick keycode.
  • Everytime you open and close a panel/menu/canvas and still navigating in the UI, you have to specify in your code to clear the current highlight then set the one you want for the menu you’re in.
  • To be clear, this script doesn’t click on the button, it highlight/select a button you want to start your navigation from with a gamepad (or any controller link to the X & Y axis)

Don’t forget to switch from Automatic to Explicite in the button navigation section and then choose which button is connected to which button.