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.

1 Like

Try calling Select() on your button. myButton.Select();

–

Other solution above (calling OnSelect(null)) didn't work, but ^Wouter_Esken's solution using a Coroutine worked for me!

–

6 Answers

6

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

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

–

to get my toggle to toggle, I had to use myButton.OnSubmit(null) instead of myButton.OnSelect(null);

–

Hi @Ipefix This worked perfectly for me, but may I ask why this part? myButton.OnSelect(null); // Or myButton.OnSelect(new BaseEventData(EventSystem.current))

–

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

–

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.

–

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.

THANK YOU! :slight_smile:

This worked for me:

startButton.Select ();
startButton.OnSelect (null);

How does one do with with Toggles instead of buttons it doesn't seem to be working for me?

–

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

Nice explanation, thank you !

–

Thank you :)

–

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.