How to change selected button in EventSystem or deselect it

Hello guys!

My problem is that whenever I click on the UI button, it gets selected under EventSystem, so when I click Space after clicking a button it will automaticaly click that button again. I’d like to change the selected button to some other button or deselect it after being clicked.

39340-screenshot-1.jpg

That’s how the EventSystem looks before clicking anything.

39341-screenshot-2.jpg

And that’s how it looks after clicking a button, so if i click Space when button (in this case PauseButton) is selected it’ll get clicked.
Possible solution would also be that nothing happens when Space is clicked.

Had the same issue. This worked for me:

GameObject myEventSystem = GameObject.Find("EventSystem");
myEventSystem .GetComponent<UnityEngine.EventSystems.EventSystem>().SetSelectedGameObject(null);

On your Button-Component you find Navigation being set to Automatic. If you set it to None, your Button won’t be selected after you clicked it.
For more information, read here: Navigation Options
153733-navigation-none.png

Be aware that you won’t be able to use a controller or keyboard to navigate to this button.
If you want to use a controller/keyboard, you need to deselect your button through code instead ( EventSystem.current.SetSelectedGameObject(null); ).

Update for 5.5 onward:

UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject (null);

I was able to get this functionality using the existing EventTrigger component by adding a Pointer Exit event that calls the scene’s EventSystem’s SetSelectedGameObject.

The event raises a ArgumentException: failed to convert parameters if you don’t supply a GameObject (can’t set it to null/none sadly). To get around this, I just pass in the EventSystem GameObject instead. This probably won’t cause any problems, but you could always pass in an empty GameObject instead of the EventSystem.

This method also lets you copy/paste the component around and so far works correctly.

52018-capture.png

Check the EventSystem API here. It seems what you need is EventSystem.SetSelectedGameObject.