Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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
8
Question by Wouter_Eskens · Mar 23, 2016 at 04:45 PM · eventsystembuttonstatesselected

EventSystem.SetSelectedGameObject doesn't highlight button

A button doesn't become highlighted when I use EventSystem.SetSelectedGameObject(gameObject). The EventSystem itself says the button is selected but it doesn't display the highlighted color.

Comment
Add comment · Show 2
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 flashframe · Mar 23, 2016 at 06:24 PM 1
Share

Try calling Select() on your button.

 myButton.Select();

avatar image Wouter_Eskens flashframe · Mar 24, 2016 at 09:21 AM 0
Share

That didn't work neither.

Thank you for your answer though!

6 Replies

· Add your reply
  • Sort: 
avatar image
13
Best Answer

Answer by Wouter_Eskens · Apr 07, 2016 at 12:05 PM

I found a fix on this forum.

You should first set the selected game object to null, then you should wait for the end of the frame (using a Coroutine), then you can set the selected game object of your choice.

In this way you can force the EventSystem to highlight the gameobject you want.

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
32

Answer by Ipefyx · Jan 24, 2017 at 04:03 PM

I know this question is a bit old, but just in case; I found a solution without Coroutine by using the state modifier of the button.

In C# :

 // Select the button
 myButton.Select(); // Or EventSystem.current.SetSelectedGameObject(myButton.gameObject)
 // Highlight the button
 myButton.OnSelect(null); // Or myButton.OnSelect(new BaseEventData(EventSystem.current))
Comment
Add comment · Show 9 · 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 jjbish · Jan 31, 2017 at 10:25 PM 0
Share

Thanks for this! Never know when it will help somebody.

avatar image Ryuuguu · Apr 15, 2017 at 12:59 PM 0
Share

to get my toggle to toggle, I had to use myButton.OnSubmit(null) ins$$anonymous$$d of myButton.OnSelect(null);

avatar image MiniBeatBoy · Dec 24, 2017 at 02:08 AM 1
Share

Hi @Ipefix This worked perfectly for me, but may I ask why this part?

 myButton.OnSelect(null); // Or myButton.OnSelect(new BaseEventData(EventSystem.current))

avatar image Ryuuguu · Feb 04, 2018 at 04:45 PM 1
Share

The

 myButton.Selectnull(); //part set this athe select ui object but does not cause any actions to be called. 

The

myButton.OnSelect(null); // part cause the actions tied to OnSelect to fire- including color changes or highlighting etc. to be called. $$anonymous$$yButton.OnSubmit(null);// will cause actions tied to OnSubmit to be fired - these usually the unityAction associated with the button.

In general On... methods fire actions.

avatar image djordr · Apr 27, 2018 at 06:38 PM 0
Share

I don't get this to work. I currently got this that sets a button to be active but it's not highlighted.

        eventSystem.GetComponent<EventSystem>().SetSelectedGameObject(volume);

volume being a reference to the button. I guess that's the same as your first line. But:

 volume.OnSelect(new BaseEventData(EventSystem.current))

does nothing for me, it's not working. What exactly is myButton a reference to if not a reference to the button? I don't get it.

Show more comments
avatar image
3

Answer by juanitogan · Oct 31, 2018 at 12:25 AM

Just ran into this when reworking my menus. Basically, this is what is going on: If a Selectable Component is selected, deactivated, then reactivated without other new selections during all that, then the object is still selected but the highlighting is lost for some reason (perhaps a bug, oversight, dunno). (Selecting while deactivated probably also leads to this.) Thus, if you only try to reselect it, this does not reset the highlight because OnDeselect and OnSelect are not sent again to the old and the new objects (because nothing has changed, presumably).

This is why sending component.OnSelect(null) to the selected-but-not-highlighted Selectable Component in this scenario works. It simply adds what was lost during deactivation. (No component.Select() needed first if it really is already selected.)

If, however, you don't want to worry about finding the Selectable Component versus the GameObject, or who was what before whatever, it also hackishly works to select nothing before selecting/reselecting something:

 eventSystem.SetSelectedGameObject(null);
 eventSystem.SetSelectedGameObject(gameObject);

This should only be needed when dealing with deactivating and activating UI objects with no other Selectables selected between those states. If always loading/unloading UIs as scenes instead (or always selecting something new on a newly-activated submenu), this should not be an issue (unless your design deals with deactivated objects on load that aren't woken up and activated in the proper order).

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 Ipefyx · Dec 05, 2018 at 01:23 PM 1
Share

Nice explanation, thank you !

avatar image
2

Answer by psychicparrot · Feb 01, 2018 at 02:52 PM

THANK YOU! :)

This worked for me:

 startButton.Select ();
 startButton.OnSelect (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 Davonafe · Dec 18, 2018 at 08:05 PM 0
Share

How does one do with with Toggles ins$$anonymous$$d of buttons it doesn't seem to be working for me?

avatar image
1

Answer by dylanfries · Jan 09, 2021 at 07:16 PM

I always thought this was a bug but I guess seeing it still here after many years it turns out it was a feature.

One thing I would add is that I noticed this only works if the UI is already open, so you can't set the selected state while it is disabled and then enable it. Just a little gotcha for anyone still stuck.

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
  • ›

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

17 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

Related Questions

Android Button : Select (highlight) by touch, before clicking. 2 Answers

Highlight button via script at runtime 5 Answers

Move through selected buttons one by one 0 Answers

EventSystem.SetSelectedGameObject() does not highlight previously selected object 2 Answers

InputField Weird behaviour 0 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