Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Mysterious-Hercules · Jan 21, 2020 at 01:07 AM · uibuttonbug-perhapseventsystemselectable

Selecting a selectable using script doesn't seem to work anymore?

Hello everyone,

I'm trying to write a system that automatically selects a "selectable" variable after a menu is instantiated. This way there's always a button selected at the beginning.

Here's my function that selects UI Elements:

 public void SelectElement(Selectable input)
 {
       Debug.Log("Selecting element: " + input.gameObject.name);
       input.Select();
       EventSystem.current.SetSelectedGameObject(input.gameObject);
 }

And Here's where I call it:

 if (CurrentUI.FirstSelectedObject)
  {
      //TODO selecting object should be here
      Debug.Log("Selecting first object");
      SelectElement(CurrentUI.FirstSelectedObject);
 }

And nothing seems to work. Even though both Debugging Messages print correctly and I checked all of the following:

  1. I checked if there are any error or warning messages, and nothing prints.

  2. I checked that CurrentUI and FirstSelectedObject are indeed the button I want to select.

  3. I tried calling this function one time at in Start(), and one time in Update(), and one time in both.

  4. I tried using only input.select(), and I tried using only EventSystem.SetSelected

  5. I have checked that there's an event system that works normally. In fact navigation works perfectly once I click on a button using a mouse instead of the script.

This is bothering me because I have used this code before and It worked with that same system. Is this a bug? or did I forget something?

Update:

I also noticed something interesting. When I click on the event system it displays the following on the top: "Selected:Button (UnityEngine.GameObject)"

So it clearly selects it, but the selected color doesn't display, and I can't click submit to press it (even though I normally can when I select manually and press submit after that). OnSelect also doesn't get called.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zobotron · Oct 29, 2021 at 01:56 PM

I had the same problem and the solution is to keep the state of your selectable and update them in Update

 public class GameLoaderCanvas : MonoBehaviour
 {
     enum SelectedButton
     {
         Load,
         New,
         Quit,
         None
     }
 
     [SerializeField] Button loadButton;
     [SerializeField] Button newButton;
     [SerializeField] Button quitButton;
 
     SelectedButton selectedButton = SelectedButton.None;
 
     private void Start()
     {
         loadButton.Select();
         selectedButton = SelectedButton.Load;
     }
 
     private void Update()
     {
         switch (selectedButton)
         {
             case SelectedButton.Load:
                 loadButton.Select();
                 break;
             case SelectedButton.New:
                 newButton.Select();
                 break;
             case SelectedButton.Quit:
                 quitButton.Select();
                 break;
             case SelectedButton.None:
                 break;
         }
     }
 
     void OnUp()
     {
         Debug.Log("Up");
 
         switch (selectedButton)
         {
             case SelectedButton.Load:
                 selectedButton = SelectedButton.Quit;
                 break;
             case SelectedButton.New:
                 selectedButton = SelectedButton.Load;
                 break;
             case SelectedButton.Quit:
                 selectedButton = SelectedButton.New;
                 break;
             case SelectedButton.None:
                 selectedButton = SelectedButton.Load;
                 break;
         }
     }
 
     void OnDown()
     {
         Debug.Log("Down");
 
         switch (selectedButton)
         {
             case SelectedButton.Load:
                 selectedButton = SelectedButton.New;
                 break;
             case SelectedButton.New:
                 selectedButton = SelectedButton.Quit;
                 break;
             case SelectedButton.Quit:
                 selectedButton = SelectedButton.Load;
                 break;
             case SelectedButton.None:
                 selectedButton = SelectedButton.Load;
                 break;
         }
      }
 }

I hope this can help.

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

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

NullReferenceException after passing InputField gameObject into EventSystem.SetSelectedGameObject() 1 Answer

UI Element Got & Lost Focus Handling 2 Answers

Button is not being clicked. 1 Answer

Can I Simulate the "Submit" Event with Gamepad Buttons? 0 Answers

Button.Select(); does not highlight? 12 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