DEV Community

loading...

How to get text from Input field in Unity3D with C#

davidkou profile image XinyuStudio ・1 min read

https://i.stack.imgur.com/CG6zo.gif

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);
    }
}

Discussion (0)

pic
Editor guide