TextField not receiving keyboard events?

I’ve attached the below code to a GameObject in my game.

The GUI displays correctly, however, when I try to type into a TextField, no text appears. The game behind the GUI is still receiving correct events which I’m reading via Input.GetAxis, so it appears that the TextField/GUI objects arent getting any keyboard events?

Am I missing something simple?

function OnGUI() {
    var member_name = PlayerPrefs.GetString("member_name");
    if(member_name == "") {
        GUILayout.BeginArea(Rect((Screen.width / 2)-300, (Screen.height / 2)-100, 600, 400));
        GUILayout.BeginHorizontal();
        GUILayout.Label("What is your name?");
        member_name = GUILayout.TextField("", GUILayout.Width(300));
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        GUILayout.Label("What is your email address? (optional)");
        var email_address = GUILayout.TextField("", GUILayout.Width(300));
        GUILayout.EndHorizontal();
        GUILayout.Button("save my high score");
        GUILayout.EndArea();
    }

OnGUI is called every time the GUI needs re-rendering. Like if input is received. The GUI controls are re-evaluated and re-generated on each cycle - not just created once and then referenced like traditional GUI systems.

In your text field line:

member_name = GUILayout.TextField("", GUILayout.Width(300));

You’re doing everything right had it been a traditional instantiate and reference GUI system. However since we’re in Unity, you need to think as the GUI being more abstract - more of a flow.

The GUI will be re-rendered when needed and the way to control the rendering is to have variables from outside the function affect its flow.

Therefore the first thing you want to do is to define member_name in class scope. That way it is properly updated and stored. After this, you need to correctly update it though the text field like so:

member_name = GUILayout.TextField( member_name, GUILayout.Width( 300 ) );

Should member_name be altered by some other method outside of OnGUI, the GUI system will call OnGUI again to re-render and thus reflect the changes caused by that variable.