- Home /
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#
Answer by WoozyBytes · Apr 14, 2015 at 03:49 PM
Hi i Think You are looking For something Like This Selectable.Select()
UnityEngine.UI.Button btn
;
btn.Select();
More details on UI Buttons can be found on Unity Docs.
Hope it Helps.
This is exactly what i used to highlight the previously selected buttons when reloading my scene.
Dislike, this does not work. The button is getting selected, but not hightlighted.
$$anonymous$$agicstyle, I just tested Button.Select(); and it doesn't execute the code on the button. It highlights it as the selected selectable. It works.
Answer by Ipefyx · Dec 15, 2017 at 04:30 PM
HI, I know this question is a bit old but, if that may help, you can check my answer on an other topic https://answers.unity.com/answers/1303493/view.html
This was extremely helpful, thank you. Was stuck with a button that was being selected but not properly highlighting, when others would.
Adding the "button.OnSelect(null);" helped fix it. $$anonymous$$uch love!
Answer by Scuffed · Oct 27, 2016 at 11:10 PM
When using this code, it states btn is null..... how do you find the actual Object to assign it to the btn?
Hi there. You have to assign a UI button object to the "btn" variable either in the inspector or via scripting. Hope it helps Regards.
Answer by BdarVirtu · Dec 02, 2020 at 04:03 PM
Hi, any advice on how to deselect a button?
You can deselect everything with EventSystem.current.SetSelectedGameObject(null); If that suits your needs.
Answer by GamesOfArcadia · Mar 14, 2021 at 12:49 PM
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.
Your answer
Follow this Question
Related Questions
Buttons stopped working after I temporarily changed the font. 1 Answer
Canvas-prefab elements are not interactable 2 Answers
EventSystem.SetSelectedGameObject Does not display highlight animations for my buttons 1 Answer
Canvas buttons too slow 0 Answers
EventSystem.SetSelectedGameObject() does not highlight previously selected object 2 Answers