Get text from Input field in Unity3D with C#

Mirko Brombin picture Mirko Brombin · Feb 2, 2015 · Viewed 75.4k times · Source

I'm trying to get a text inside an inputField in Unity3D with C#.

I've placed an inputField in my editor, renamed and tagged in: Username_field.

My question is: How i can get the text inside the InputField Username_field in a C# script?

Answer

David picture David · Feb 2, 2015

Attach below monobehaviour script to your InputField gameObject:

public class test : MonoBehaviour {
    void Start ()
    {
        var input = gameObject.GetComponent<InputField>();
        var se= new InputField.SubmitEvent();
        se.AddListener(SubmitName);
        input.onEndEdit = se;

        //or simply use the line below, 
        //input.onEndEdit.AddListener(SubmitName);  // This also works
    }

    private void SubmitName(string arg0)
    {
        Debug.Log(arg0);
    }
}

See also below animation:

enter image description here