Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
10
Question by JtheSpaceC · Feb 16, 2016 at 04:50 PM · uibuttoneventsystemselect

Button.Select(); does not highlight?

Hi.

For using a gamepad I have what ought to be a handy script going. When a menu comes up, I want a specific button to be selected and highlighted so the player can start navigating.

I put the following script on a GameObject (like the panel Parent that contains the buttons) which is inactive in the scene (or it's activeSelf but its parent is inactive, either way..).

 using UnityEngine;
 using UnityEngine.UI;
 
 public class ButtonSelector : MonoBehaviour {
 
     public Button selectButton;
 
     void OnEnable()
     {
         if(selectButton != null)
         {
             selectButton.Select();    
             print("selected button");
         }
         else
             Debug.Log ("SelectButton was null");
     }
 }

The button does select, as in if I press a Submit button (spacebar, enter, Gamepad A, etc) it will activate, but the button is usually not highlighted. If I start to navigate, like move on to the next button, the next button does highlight (and select) and then I can move back to the original and it will appear highlighted. But why doesn't it highlight the first time?

Is Select() meant to select AND highlight? If not, what do I use?

Thanks Kevin

Comment
Add comment · Show 6
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 vintar · Feb 16, 2016 at 05:41 PM 0
Share

You could try :

 using UnityEngine.EventSystems;
 
 void OnEnable()
 {
     if(selectedButton != null)
     {
         EventSystem.current.SetSelectedGameObject(selectedButton, new BaseEventData());
     } 
 }
avatar image JtheSpaceC vintar · Feb 17, 2016 at 08:08 PM 0
Share

Thanks but that didn't do it.

First of all the line there had to be

         EventSystem.current.SetSelectedGameObject(selectedButton.gameObject, new BaseEventData(EventSystem.current));

assu$$anonymous$$g that 'selectedButton' was a Button and not a GameObject, and the BaseEvent Data needed an argument. I assume you meant to give it the current system?

Thanks but this resulted in the exact same behaviour as before. The buttons would be selected (proven by the fact that 'Spacebar' or other Submit buttons would activate them) but only some of them were highlighted. Oddly, it's always the same ones do or don't highlight, but there doesn't seem to be an overall rule (such as submenus not working or whatever).

avatar image dppc · Feb 17, 2016 at 09:29 PM 0
Share

Select() should select and highlight.

Have you tested in the editor by manually enabling the GameObject that script is on (via its checkbox in the Inspector)? Does it work consistently then?

avatar image JtheSpaceC dppc · Feb 18, 2016 at 10:22 AM 0
Share

Good to know Select() should highlight. Just in case I was using it wrong.

Your suggestion doesn't work but it's shown me that there's definitely buggy behaviour going on. When I launch, I get the nomal button-is-selected-but-you-can't-see-it behaviour, but after I disabled the panel via the inspector, the button didn't come back on. It wasn't even clickable. In fact, loading up any scene in the game and trying to use the UI just didn't work. All buttons stopped behaving as clickable. Something must be breaking the Event System.

The only way to clear the error was to restart the entire editor.

Now if I play the game as normal, stop the game, and re-enter play mode, none of the buttons respond. I can basically only get one play out of the game in the editor without a restart. Crazy behaviour! (and yes the Event System is still in the scene, active)

I'll open a ticket. This was the same with versions 5.3.2p2 and 5.3.2f1. It seemed in both that as soon as I tried your suggestion it broke something forever. Time.timeScale was 0 as I did this (it was a pause menu).

avatar image JtheSpaceC JtheSpaceC · Feb 18, 2016 at 11:16 AM 0
Share

Actually, it's broken input across all projects.

I've uninstalled and reinstalled Unity. Tried 5.3.2f1, 5.3.2p2 and 5.3.2p3 all with clean installs. Any project that I open behaves thus:

On first Editor play the button systems seems to respond normally (until a scene change anyway). On second editor play no buttons respond to navigation or selection.

All projects, clean installs, different versions of Unity so it's not likely a Project Settings thing. This also makes it a HUGE blocker to my development process, especially considering my next build focuses on UI. I've opened a ticket but, HELP!! $$anonymous$$AYDAY!! :P

Also, Time.timescale didn't seem to affect anything as I got the same behaviour at '1' as at '0'.

Show more comments

12 Replies

· Add your reply
  • Sort: 
avatar image
14

Answer by noio · Mar 11, 2016 at 10:42 AM

I am not happy with this, but I solved it by waiting a frame before selecting the button.

     IEnumerator SelectContinueButtonLater()
     {
         yield return null;
         _eventSystem.SetSelectedGameObject(null);
         _eventSystem.SetSelectedGameObject(continueButton.gameObject);
     }
Comment
Add comment · Show 6 · 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 xxZap · Jun 13, 2016 at 12:41 PM 0
Share

This is terrible butIi'm very glad to solve my issue. Thank you.

PS: so it's a Unity bug.

avatar image mirrorlink · Jun 21, 2016 at 12:27 AM 0
Share

THAN$$anonymous$$S FOX!

avatar image kamathaj · Nov 03, 2018 at 09:16 AM 0
Share

This is definitely a bug. Thanks for this fix though!

avatar image wlwl2 · Aug 26, 2019 at 11:43 PM 0
Share

I've tried many other solutions over a long period of time, this one seems to finally work for me. Thank you!

avatar image Bvenjamin · Nov 22, 2020 at 06:21 PM 0
Share

using 2019.4 and the new input system, it did not highlight when I called Select when I had an object targeted in the EventSystem's "First Selected" field, but when I set it to None I no longer had to wait a frame for the highlight to work.

Show more comments
avatar image
7

Answer by valtterip · Nov 11, 2016 at 08:32 AM

Ran into this and tried different solution proposals, got it working with the yield-workaround, but it didn't seem to make much sense. So I noticed you don't need to wait the frame, just reset the selected GO before setting it selected again. So something along these lines does the trick for me

 private void setButtonsActive(bool set)
 {   
     buttons.SetActive(set);
     if (set)
         EventSystem.current.SetSelectedGameObject(buttons.GetComponentInChildren<Button>().gameObject, null);        
     else
         EventSystem.current.SetSelectedGameObject(null);
 }
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 angrypenguin · Nov 25, 2016 at 04:05 AM 0
Share

I had something similar myself...

In my case I had a GUI that I was turning on and off dy deactivating and re-activating a root level GameObject as necessary. The first time I opened it in this fashion it worked fine. If I closed and re-opened it the default button would behave as if it were selected, but would not be highlighted even after re-setting the selection.

$$anonymous$$y guess is that the EvenySystem checks to see if the GameObject is already selected before it applies the selection, but that the Button script loses its highlight when deactivated and reactivated. With this in $$anonymous$$d, as with @valtterip's post, I found that nulling the selection and re-applying it seemed to do the trick.

This suggests a $$anonymous$$or issue with the Button component, if my guess above is correct. If it is supposed to maintain its selected status through re-activation then it should re-apply its highlight appropriately (I would assume this is the intent). If it's not supposed to maintain its selected status then it should de-select itself upon deactivation so that the overall system has a consistent representation of its current state.

avatar image
4

Answer by mashiro11 · Apr 28, 2020 at 04:10 PM

I know it's a really old post, but to anyone who like me ends up here..

It is not a bug. EventSystem is a component, just another component. Unity lifecycle says that on initializaton, all components Awake and OnEnable are called (in this order), only then all components Start is called.

So, EventSystem Awake and OnEnable may not have been called when our button.Select() was called* if we do it inside Awake or OnEnable (I had this kind of trouble with my components, where one needs to be ready first for others to start calling it).

Also, if your button game object is not enabled, trying to do

 button.Select(); //button is a Button component 
 button.gameObject.SetActive(true);

won't work either (lost some good hours with this). Instead, make sure the gameObject is enabled first

 button.gameObject.SetActive(true);
 button.Select();

  • of course, if your button.Select() is being called some point after scene initialization, EventSystem might probably be there ready to go.

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
avatar image
3

Answer by Pythalic · Apr 04, 2017 at 02:58 PM

For anyone who doesn't like the presented workaround:

Putting Select() or EventSystem.SetSelectedGameObject(...) into the Start() method, rather than Awake() or OnEnable() solved it for me.

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 thatCamelCode · Feb 28, 2018 at 02:59 AM 0
Share

Yes, this is what I've been doing in my projects where I allow the use of a controller for navigation. I get the thinking that you want to do it as soon as possible, so you put it in OnEnable, but visually the user won't notice the difference between whether it's in OnEnable or in Awake/Start. Personally, I also see this method as being a lot nicer than a fiddle with a coroutine.

avatar image
3

Answer by Arclite83 · May 11, 2020 at 08:23 AM

I also wasn't a huge fan of the above solutions. I have no idea if this is correct or ideal but it solves the problem of "I toggle a menu closed/open so I want to re-select my default button, but if I was already ON that button it shows as not selected".

 private void ForceSelectGameObject(GameObject gameObject)
 {
     if (EventSystem.current.currentSelectedGameObject == gameObject)
     {
         EventSystem.current.SetSelectedGameObject(null);            
     }
     EventSystem.current.SetSelectedGameObject(gameObject);    
 }
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
  • 1
  • 2
  • 3
  • ›

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

68 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 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 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 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 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

Button is not being clicked. 1 Answer

Can I Simulate the "Submit" Event with Gamepad Buttons? 0 Answers

How to change selected button in EventSystem or deselect it 5 Answers

Can I use EventSystem + InputModule to implement scroll via button? 0 Answers

UI button not working 1 Answer


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