Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
9
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
34
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 7
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
20

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 alt text
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); ).


navigation-none.png (24.0 kB)
Comment
Add comment · Show 5 · 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 · Jan 14, 2021 at 08:02 AM 0
Share

Best answer

avatar image neo-mashiro · Jun 15, 2021 at 04:02 AM 0
Share

Thank you so much for the answer, it saved me a lot of time.

avatar image Fenikkel · May 03 at 02:33 PM 0
Share

Perfect for mouse or touch controls, but broke the navigation with controllers right?

avatar image florian85vr · May 12 at 12:09 PM 0
Share

Worked perfectly. Thank you.

avatar image
4

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

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.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image

Related Questions

Button is not being clicked. 1 Answer

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

Detect UI Button Click Event in Update method 3 Answers

First selected GameObject not highlighted 3 Answers

Animator Not Playing on Button Click 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges