• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
8
Question by Denis-Crnic · Jan 23, 2015 at 07:05 AM · buttonclickeventsystemselectdeselect

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.

alt text

That's how the EventSystem looks before clicking anything.

alt text

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.

screenshot-1.jpg (3.4 kB)
screenshot-2.jpg (6.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
31
Best Answer

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);
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rlatapy · Oct 11, 2016 at 03:52 PM 4
Share

Work for me, you can use EventSystem.current ins$$anonymous$$d of GameObject.Find("EventSystem");

 EventSystem.current.GetComponent<EventSystem>().SetSelectedGameObject(null);

avatar image SergeantBiscuits rlatapy · Mar 11, 2017 at 02:30 AM 5
Share

Can simplify it even a little more:

 EventSystem.current.SetSelectedGameObject(null);
avatar image aaronfranke SergeantBiscuits · Nov 17, 2017 at 01:33 AM 0
Share

error CS0103: The name `EventSystem' does not exist in the current context.

Show more comments
avatar image
2

Answer by frarees · Apr 09, 2015 at 09:01 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

alt text


capture.png (9.9 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by Terkish1987 · Apr 10, 2017 at 08:20 PM

Update for 5.5 onward:

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
10

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 Buttons won't be selected anymore, after you clicked them. For more information read here: Navigation Options alt text


navigation-none.png (24.0 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Insok · Jul 26, 2020 at 08:42 PM 1
Share

This should be the accepted answer, this solution is much more elegant.

avatar image asrichesson · 3 days ago 0
Share

Best answer

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Button is not being clicked. 1 Answer

Button.Select(); does not highlight? 10 Answers

First selected GameObject not highlighted 3 Answers

Detect UI Button Click Event in Update method 3 Answers

Invoke other UnityEvent with gameobject.GetComponent().onClick.AddListener(); 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges