Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by nanella · May 21, 2018 at 08:16 PM · uiinputfieldactivatefocusselect

inputField.ActivateInputField() not working

Hello,

I am trying to focus an InputField. At another place in my game I got the following to work (the InputController is attached to the InputField GameObject, which is active):

 public class InputController : MonoBehaviour
 {
         private InputField inputField;
         
         public void Setup()
         {
                     inputField = gameObject.GetComponent<InputField>();
                     inputField.ActivateInputField ();
                     inputField.Select ();
                     Debug.Log("activated input field: " + inputField.isFocused);
         }
 }

But in this case the InputField does not get activated (as the caret is not showing) and inputField.isFocused is logged with false. Anybody got an idea why the InputField does not get focused? Any help is appreciated.


EDIT: I've looked a little more into what is happening:

  • If I call inputField.isFocused inside of Update, it remains false even some frames after Setup() has been called.

  • The inputField is set to interactable.

  • The placeholder is showing at the correct position inside the input field, so is the actual user input. If I click the inputField, the caret is showing at the correct position, too.

  • When looking at the input field in the editor, it has a child named InputField(Clone) Input Caret. This has the same position as the placeholder (which is shown correctly), but is not showing. If I click into the inputField, the caret is showing, but the GameObject of the caret does not change at all.

  • UnityVersion used: 2017.3.1f1 Personal


EDIT: The code that calls Setup() is as follows:

 private GameObject AddInputGameObject()
 {
     GameObject inputField = Instantiate (inputFieldPrefab, panel.transform);
 
     Text placeholder = inputField.transform.Find("Placeholder").GetComponent<Text>();
     placeholder.text = "Please enter sth";
     
     InputController inputController = inputField.GetComponent<InputController>();
     inputController.Setup();
     
     return inputField;           
 }
Comment
Add comment · Show 11
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 Harinezumi · May 23, 2018 at 09:45 AM 1
Share

According to this forum thread, you are doing it correctly, but you might need to wait a frame for the input field to really get selected and focused.

avatar image nanella Harinezumi · May 24, 2018 at 08:04 AM 0
Share

Unfortunately after waiting some time, the input field still has no focus, so waiting a frame does not seem to work :/

avatar image Harinezumi nanella · May 24, 2018 at 08:15 AM 1
Share

I just tested it, and it works for me: with the above calls InputField gets focused, and caret is showing. In fact, either of those calls is enough to focus it and show the caret. Note that isFocused remains false, because it becomes true only in the next frame.

Is the InputField set to interactable? Did you change the size of the text area inside it? That is, is the text area big enough for the caret to show? Also, what version of Unity are you using? I tested it on 2017.3.1f1.

Show more comments
avatar image Harinezumi · May 24, 2018 at 09:41 AM 1
Share

I've created a $$anonymous$$imal project to test this, see attached unitypackage. It works fine for me, with exactly the code that you've posted, so I think something takes away the focus from your InputField. Could you test the $$anonymous$$imal project if it works for you?

inputfieldfocustest.zip (5.2 kB)
avatar image nanella Harinezumi · May 24, 2018 at 09:58 AM 0
Share

Your $$anonymous$$imal project works for me, too. I really wonder what keeps my inputField from focusing. $$anonymous$$aybe something really is "s$$anonymous$$ling" the focus from it, but shouldn't it show that it is focusing in the logs at least once?

avatar image Harinezumi nanella · May 24, 2018 at 10:09 AM 1
Share

Perfect! What if you comment out placeholder.text = "Please enter sth";? I have a hunch that setting the placeholder text may s$$anonymous$$l the focus.
Although, in my test that didn't change anything... :(

Show more comments

2 Replies

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

Answer by nanella · May 24, 2018 at 10:38 AM

Found it out myself after @Harinezumi pointed me in the right direction by saying that the code made sense but something else might affect the input field.


Solution: The InputField was part of a panel which was inactive and only set active after the Setup()-call that should have focused the InputField. Seems the GameObject of the InputField has to be active before the focusing can work.


Lesson learned: Always double-check active and inactive GameObjects.

Comment
Add comment · Show 3 · 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 Harinezumi · May 24, 2018 at 10:56 AM 1
Share

Perfect, I'm glad you found the solution! And now we also know that you can only select Selectables who are active in the hierarchy.

avatar image Adam_Streck · Sep 25, 2020 at 10:09 AM 1
Share

If anyone is curious, this also applies during the OnEnable, so you need to skip a frame if you want to activate in OnEnable.

avatar image ryanwiesjahn Adam_Streck · Oct 20, 2020 at 02:35 AM 0
Share

Thank you! This is what my problem was, trying to set focus on an input field during OnEnable. Delaying a frame fixed my issue.

avatar image
-1

Answer by oussamahassini6 · May 23, 2018 at 09:50 AM

so You can download the textMeshPro features it might help you pls a good vote @nanella

Comment
Add comment · Show 3 · 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 nanella · May 24, 2018 at 08:03 AM 0
Share

What is text$$anonymous$$eshPro? And how will it help me?

avatar image Harinezumi nanella · May 24, 2018 at 08:10 AM 0
Share

Text$$anonymous$$eshPro is a text rendering solution for Unity, that was paid before, but now is free with Unity (thanks to Unity Technologies buying it). You can get it from here: https://www.assetstore.unity3d.com/en/#!/content/84126
I'm not sure it will solve your problem, but it is a really good asset.

avatar image nanella Harinezumi · May 24, 2018 at 09:13 AM 0
Share

Thanks for the advice, I may try Text$$anonymous$$eshPro out

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

144 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

Related Questions

Focus on/select (TMP) input field? 2 Answers

InputField disable selecting 1 Answer

UI Button Focus 2 Answers

Why Android device need to loose Inputfield focus to listen to others events ? 0 Answers

InputField instance isFocused always return false. 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