- Home /
Focus on text field on load
I know this is a very simple question and has probably been asked before, but how do I have a text field be selected as soon as I start a scene. For example, when I load my scene I want the text field to be selected as soon as you start so that you can enter your password without having to actually click on the box.
Answer by Jesse Anders · Sep 06, 2010 at 04:27 PM
I can't say with certainty that this is the 'correct' answer, but here's something you could try:
GUI.SetNextControlName("Text1");
text1 = GUILayout.TextField(text1);
GUI.SetNextControlName("Text2");
text2 = GUILayout.TextField(text2);
if (GUI.GetNameOfFocusedControl() == string.Empty) {
GUI.FocusControl("Text1");
}
Each control is assigned a name. If no control is currently focused (as determined by using GetNameOfFocusedControl()), the first control is focused (in your case you would focus the 'password' control).
You may need to add some additional logic to get exactly the behavior you're after, but maybe this will at least get you pointed in the right direction.
Sweet, all I did was set a name for the textbox and then at the awake of the script I focused the textbox. Thanks so much!
Your answer
Follow this Question
Related Questions
Input Field text history/previous 0 Answers
Activate a text field on key press 0 Answers
Input.anyKeyDown is disabled when in GUI Textfield - Help 1 Answer
How can I release gui focus via code? 6 Answers
Text field data entering 0 Answers