- Home /
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 afterSetup()
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;
}
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.
Unfortunately after waiting some time, the input field still has no focus, so waiting a frame does not seem to work :/
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.
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?
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?
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... :(
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.
Perfect, I'm glad you found the solution! And now we also know that you can only select Selectable
s who are active in the hierarchy.
If anyone is curious, this also applies during the OnEnable, so you need to skip a frame if you want to activate in OnEnable.
Thank you! This is what my problem was, trying to set focus on an input field during OnEnable
. Delaying a frame fixed my issue.
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
What is text$$anonymous$$eshPro? And how will it help me?
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.
Thanks for the advice, I may try Text$$anonymous$$eshPro out
Your answer
![](https://koobas.hobune.stream/wayback/20220612163137im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
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
Input fields act abritrarily 0 Answers
Focus on/select (TMP) input field? 2 Answers