- Home /
EventSystem.SetSelectedGameObject() does not highlight previously selected object
I am using EventSystem.SetSelectedGameObject() to set buttons as selectable and to appear highlighted in my UI. If I switch to a new screen/menu with no selectable items, like maybe an information dialog with only text for example, and then I switch back to the previous screen/menu, when I do EventSystem.SetSelectedGameObject() on the previously selected object, it is not being highlighted.
My guess is that under the hood, the EventSystem is recognizing that the object SetSelectedGameObject is trying to select is already the currently selected one, and therefore not running any "SetHighlighted" function. The object is still actually selectable - if you hit the submit/enter key, it's OnClick function does get called. It just does not get put into a visually highlighted state.
Is this expected behaviour, or have I found a bug?
I have a workaround which is to temporarily set EventSystem.SetSelectedGameObject(null) for one frame, then back to the desired selected object. This seems to force it to set it into highlighted state.
This is happening to me too, I'd really love if someone had an answer ;-; Thanks for the workaround, though. I haven't thought of that. c:
Answer by Southgarden116 · Nov 11, 2015 at 01:16 AM
Maybe you should put the code where you select your gameobject in the OnEnable() function. It gets called whenever an object is becoming active/enabled. If that's not it, where do you call the SetSelectedGameObject() function?
Answer by Ipefyx · Jan 25, 2017 at 08:19 AM
I got a similar situation where my initially selected button is not highlighted when I display my menu.
I don't know either if it's a bug or if it's on purpose but you can trick this problem by manually highlight in script when you selected your button.
EventSystem.SetSelectedGameObject(button.gameObject); // Set button as selected one
button.OnSelect(null); // Manually call OnSelect transition