- Home /
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.
That's how the EventSystem looks before clicking anything.
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.
Answer by xDavidLeon · Apr 09, 2015 at 10:58 AM
Had the same issue. This worked for me:
GameObject myEventSystem = GameObject.Find("EventSystem");
myEventSystem .GetComponent<UnityEngine.EventSystems.EventSystem>().SetSelectedGameObject(null);
Work for me, you can use EventSystem.current ins$$anonymous$$d of GameObject.Find("EventSystem");
EventSystem.current.GetComponent<EventSystem>().SetSelectedGameObject(null);
Can simplify it even a little more:
EventSystem.current.SetSelectedGameObject(null);
error CS0103: The name `EventSystem' does not exist in the current context.
Answer by Vivien_Lynn · Mar 06, 2020 at 08:56 AM
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
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);
).
This should be the accepted answer, this solution is much more elegant.
Thank you so much for the answer, it saved me a lot of time.
Perfect for mouse or touch controls, but broke the navigation with controllers right?
Answer by Jeff-Rosenberg · Aug 13, 2015 at 10:11 PM
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.
Answer by frarees · Apr 09, 2015 at 09:01 AM
Check the EventSystem API here. It seems what you need is EventSystem.SetSelectedGameObject.
Answer by Terkish1987 · Apr 10, 2017 at 08:20 PM
Update for 5.5 onward:
UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject (null);
Your answer
Follow this Question
Related Questions
Detect UI Button Click Event in Update method 3 Answers
First selected GameObject not highlighted 3 Answers
Button is not being clicked. 1 Answer
Button.Select(); does not highlight? 12 Answers
Implementing IPointerClickHandler interface does not seem to work 7 Answers