Hack Forums
[C#] (Unity 3D) Text field problem - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] (Unity 3D) Text field problem (/showthread.php?tid=5230044)



(Unity 3D) Text field problem - Yurt - Apr 7, 2016

So I'm messing around trying to create a small game and I've gotten stuck on something very annoying... I've made a SUPER messy script that will allow you to enter your username and then it'll be displayed.. However, I can't figure out how to deselect the textfield after I press enter/return
(I don't want to click outside the textbox with my mouse)

It just creates another line (Which should definitely NOT happen)
I've went through the first 2 pages of google for an answer and NOTHING is working for me......

So someone please help me out so if I press enter it deselects the text field and loads the next level..
(Bonus points if the textfield is automatically activated on start)

Code
using UnityEngine;
using System.Collections;

public class Username : MonoBehaviour {

    public static string name;
    public string stringToEdit = "";

    void OnGUI() {

    
        GUIStyle myCustomStyle = new GUIStyle ();
        GUI.skin.textArea.normal.background = null;
        GUI.skin.textArea.active.background = null;
        GUI.skin.textArea.onHover.background = null;
        GUI.skin.textArea.hover.background = null;
        GUI.skin.textArea.onFocused.background = null;
        GUI.skin.textArea.focused.background = null;
        GUI.skin.textArea.normal.textColor = Color.white;
        stringToEdit = GUI.TextField(new Rect(10, 10, 200, 20), stringToEdit, 9, myCustomStyle);

        
    }
    
    void Update()
    {
        Finished ();
    }

    void Finished()
    {
        if (Input.GetKeyDown ("return"))
        {
            name = stringToEdit;
            Application.LoadLevel(1);
        }
    }

}



* - severos - Apr 8, 2016

Well, from the inspector you can pick the type of the input field, far as I remember there was standard (with single/multi line option)/ password/ PIN/ etc.
And since you're making it with code you'll need to find it in the documentation and see what's the class having it and how to set it.
As for selecting the field on load, you can try the methods mentioned here: http://forum.unity3d.com/threads/focus-on-inputfield-programmatically.264472/ just remember to put it in the Awake function to execute once on load only.