Disabling/Hiding a Text Field with Enter Key

Here’s what I have so far…

var naming1 : boolean = false;
var slot1_namer : GUI;
var slot1_name : String;

function Update (){

     if(Input.GetKeyDown(Keycode.Return))
     {
         if(naming1 == true)
         {
         naming1 = false;
         }
         else if(naming1 == false)
         {
         naming1 = true;
         }
     }
}
function OnGUI (){
     if(naming1 == true)
     {
     slot1_namer.enabled = true;
     slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
         if(Input.GetKeyDown(Keycode.Return))
         {
         slot1_namer.enabled = false;
              slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
         }
     }
     else if(naming1 == false)
     {
      slot1_namer.enabled = false;
              slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
     }
}

redundant in the end, I know, but I was REALLY trying to get this to work. So how can I disable (or ideally “hide”) the textfield when “return” or “enter” is pressed?

EDIT: New development, every time I leave then go back to the menu with the TextFields, the ability for “enter” to enable and disable them is reset. Meaning that as long as none of the fields are edited, the code works properly and they fields enable and disable. yet if any of them are edited, this stops working so I have to back out then go back in.

How about:

public class SomeClass : MonoBehaviour {
    private bool textFieldEnabled = true;
    private string textFieldText = "";

    void update() {
        if(Input.GetKeyDown(Keycode.Return)) {
            textFieldEnabled = !textFieldEnabled;
        }
    }

    void OnGUI() {
        if (textFieldEnabled) {
            textFieldText = GUI.TextField(new Rect(0, 0, 100, 25), textFieldText);
        }
    }
}

Try removing all lines from the 24th to 28th line. Basically in the Update() you are changing the boolean to the opposite value, so if you set it to ‘false’ in OnGUI(), the Update() will turn it ‘true’.

This example might also help:

http://forum.unity3d.com/threads/188202-Simple-dynamic-list-editor

Use this code. It will solve all focus problem and key detection. @jeffreywarf

public class MyConsole : MonoBehaviour
{
    // Start is called before the first frame update
    public bool showConsole = false;
    public string guiText = "";
    void Start()
    {
        
    }

    void Update()
    {
        if (Input.GetKeyUp(KeyCode.F5)) showConsole = !showConsole;
        
    }
    private void OnGUI()
    {
        if (showConsole) {
            GUI.SetNextControlName("MyTextField");
            GUI.FocusControl("MyTextField");
            guiText = GUI.TextField(new Rect(10, 10, 1000, 20), guiText, 200); 
        }
        if (Event.current.keyCode == KeyCode.Return && showConsole)
        {
            //DO SOMETHING WITH THE TEXT AND THEN CLEAR TEXT
            //ADD SOME CODE
            guiText = "";
        }
    }
}