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 /
  • Help Room /
This question was closed 5 days ago by Goldlaser2000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Goldlaser2000 · 6 days ago · buttonprefabseventsystemkeyboardfocus

No keyboard focus on Buttons instantiated from prefabs on canvas,

Currently I am Adding keyboard controlls in order to navigate a Dialog between Player and NPC by WASD and E. I have set up unitys Input controlls for this, but I run into an issue.

When I start the dialog I am able to set the focus to the "next" button (which is not creaated from a prefab) and confirm with "e".

In the next step the Player choices are displayed by instantiating prefab Buttons (see code below)

These buttons do not gain keyboard focus although I set it explictly. I still can click them with the mouse however. I googled and experimented for six hours now but seem unable to find a solution.

maybe some of ou guys has an idea... the Code looks like this:

 void UpdateUI()
         {   
             gameObject.SetActive(playerConversant.IsActive());
  
             if(!playerConversant.IsActive())
             {
                 return;
             }
  
             conversantName.text = playerConversant.GetCurrentConversantName();
             AIResponse.SetActive(!playerConversant.IsChoosing()); 
             choiceRoot.gameObject.SetActive(playerConversant.IsChoosing()); 
  
             if(playerConversant.IsChoosing())
             {
                 BuildChoiceList();
             }
             else 
             {
                 AIText.SetText(playerConversant.GetText());
                 nextButton.gameObject.SetActive(playerConversant.HasNext());
                 //put focus on the button...this works
                 EventSystem.current.SetSelectedGameObject(nextButton.gameObject);
             }   
         }
  
         private void BuildChoiceList()
         {
           
             foreach (Transform item in choiceRoot)
             {
                 Destroy(item.gameObject);
             }
  
             foreach (DialogNode choice in playerConversant.GetChoices()) 
             {
                 GameObject choiceInstan = Instantiate(choicePrefab, choiceRoot);
  
                 choiceInstan.GetComponentsInChildren<TextMeshProUGUI>().FirstOrDefault().SetText(choice.GetText());
  
                 choiceInstan.GetComponentsInChildren<Button>().FirstOrDefault().onClick.AddListener(() =>
                 {
                     playerConversant.SelectChoice(choice);
  
                 });
             }
             // Try to set fokus to first instantiated button
             Button toSetFocusOn = choiceRoot.GetComponentsInChildren<Button>().FirstOrDefault();
             toSetFocusOn.gameObject.SetActive(true);
             EventSystem.current.SetSelectedGameObject(toSetFocusOn.gameObject);   
         }
     }


The hirarchy setup looks lke this alt text

screenshot-2022-06-07-223042.jpg (459.2 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

1 Reply

  • Sort: 
avatar image
0

Answer by Goldlaser2000 · 5 days ago

Okay, I found the solution. When I call the cleanup part of the code

foreach (Transform item in choiceRoot)

{

Destroy(item.gameObject);

}

It makes new instances of the buttons spawning in a non active state, also making them resistant to setting them active explicitly, which is quite interesting, as they also stay visible and clickable. I will therfore have to use something different than Destroy();

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

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

How do I NOT play a sound on the first selected button. 2 Answers

multiple buttons not working 0 Answers

Can't keep the InputField focused 1 Answer

Buttons inside a Scroll View doesn't allow the the View to scroll. 1 Answer

How can i add a button to the UI that works as a key?,How can I add a button to the UI that works as an specific key when I press it? 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