- Home /
Focus TMP_InputField at launch
Hello, I'm tryint to focus an TMP_InputField at beginning of my game but nothing happens. I dont get any errors on Console.
Here's what I've tried after some research :
TMP_InputField inputField =InputName.transform.GetChild(1).gameObject.GetComponent<TMP_InputField>();
inputField.Select();
inputField.ActivateInputField();
Any idea on how to fix it ?
Answer by jeromeatunity · Jul 14, 2020 at 04:48 PM
Are you sure your code runs at all? Maybe insert a Debug.Log(inputField.name);
to check in the console if the code is executed.
I tested inputField.ActivateInputField();
works in the Editor. Also a null reference error is thrown if inputField is not defined.
This code InputName.transform.GetChild(1).gameObject.GetComponent();
looks quite complex and error prone to find that component.
Maybe look at :
Using GameObject.GetComponentsInChildren
Using a Tag to define the target input field, like
StartInputField
and then retrieve its GameObject withGameObject.FindGameObjectsWithTag("StartInputField")
hth.
You were right, I was working with a null reference, thank you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613003145im_/https://answers.unity.com/themes/thub/images/avi.jpg)