Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by MistaGoustan · Apr 13, 2015 at 10:28 PM · uicanvasbuttonseventsystembuttonstates

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#

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
19

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CheesyPixel · Aug 02, 2015 at 04:16 AM 0
Share

This is exactly what i used to highlight the previously selected buttons when reloading my scene.

avatar image MagicStyle · Nov 04, 2017 at 03:25 PM 0
Share

Dislike, this does not work. The button is getting selected, but not hightlighted.

avatar image YTGameDevDave MagicStyle · Sep 21, 2018 at 05:49 PM 0
Share

$$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.

avatar image mrCrush · Feb 24, 2018 at 05:33 PM 0
Share

Thanks! It works!

avatar image
2

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Davonafe · Dec 18, 2018 at 08:05 PM 0
Share

How does one do with with Toggles?

avatar image Post_Mordom · Apr 26, 2019 at 09:04 PM 0
Share

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!

avatar image
1

Answer by Scuffed · Oct 27, 2016 at 11:10 PM

@Woozybytes

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WoozyBytes · Oct 30, 2016 at 08:13 PM 1
Share

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.

avatar image
0

Answer by BdarVirtu · Dec 02, 2020 at 04:03 PM

Hi, any advice on how to deselect a button?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UnityIco · Aug 11, 2021 at 02:10 PM 0
Share

You can deselect everything with EventSystem.current.SetSelectedGameObject(null); If that suits your needs.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges