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.
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))
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.
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:
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).
This worked for me:
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.
As @juanitogan said, the button is not reselected since it keeps its “selected” state. Unsolved problem is that the button is still selected but the highlighting is lost. I think that it is because the button is reactivated however eventSystem is not. Highlighting is a part of button so if the button is reactivated, it would set to default (maybe normal?). However, the “selected” state information is saved in eventSystem so it would not reset since it was not activated.
TL;DR: highlighting (color) in button / “selected” state info in eventSystem. In this problem, button is reactivated and reset but eventSystem is not.