Textfield problems.

hey :)

so im having some pretty annoying problems with the GUI.Textfield.

var InitSpawn : boolean = true;
var ShowInitMenu : boolean = true;
var Name : String = "John";

function OnGUI () {

    if (ShowInitMenu == true){
        if (InitSpawn == true){
            Submit = GUI.Button(Rect(Screen.width/2+5, Screen.height/2+25, 165, 25), "Finished");
            EnterName = GUI.TextField(Rect(Screen.width/2, Screen.height/2, 175, 60), "Enter A Name"); // show name field

            if (Submit){ // submit button
                if (EnterName == ""){
                    Name = "John"; // if the player didnt enter anythign default the name
                }
                else{
                    Name = EnterName;   // change the players name
                };

                ShowInitMenu = false;
            };
        }
        else{
            return;
        };          
    }
    else{
        return;
    };  
};

originally it was just the EnterName field what was a var, but i had to make the button a var and put it first so you could click it.

problem im having is that you cant edit the text field :(

i need it for my name system. when you die, it prints: "Enter A Name Died"

is the text field meant to have dynamic text in it? (able to be changed) am i using it wrong D:?

help please thanks.

2 Answers

2

you gotta keep in mind how unity handles the GUI. think of it in terms of a really slow motion.

1st frame, your textfield has the text "Enter a Name", and when the user tries to type something in the field that text gets stored into the variable called EnterName.

2nd frame, your textfield has the text "Enter a name", since EnterName didnt get stored anywhere, and wasnt inserted into the textfield.

So basically the solution to your problem would be something along these lines:

var InitSpawn : boolean = true;
var ShowInitMenu : boolean = true;
var Name : String = "John";
var EnterName: String = "Enter A Name";

function OnGUI () {
if (ShowInitMenu == true){
    if (InitSpawn == true){
        Submit = GUI.Button(Rect(Screen.width/2+5, Screen.height/2+25, 165, 25), "Finished");
        EnterName = GUI.TextField(Rect(Screen.width/2, Screen.height/2, 175, 60), EnterName); // show name field
          if (Submit){ // submit button
            if (EnterName == ""){
                    Name = "John"; // if the player didnt enter anythign default the name                }
                else{
                    Name = EnterName;   // change the players name
                };
                ShowInitMenu = false;
            };
        }
        else{
            return;
        };
              }
    else{
        return;
    };
  };

I dont understand the question. Perhaps "Ask" a new question that is unrelated to this one? Also, if the above answer is the correct one then you should specify that by clicking the V

You need to pass the text field the current value of your text.

Something like ...

var InitSpawn : boolean = true;
var ShowInitMenu : boolean = true;
var Name : String = "Enter A Name";

function OnGUI () {

    if (ShowInitMenu){
        if (InitSpawn){
            Submit = GUI.Button(Rect(Screen.width/2+5, Screen.height/2+25, 165, 25), "Finished");
            Name = GUI.TextField(Rect(Screen.width/2, Screen.height/2, 175, 60), Name);

            if (Submit){ // submit button
                if (Name == "Enter A Name"){
                    Name = "John"; // if the player didnt enter anythign default the name
                }

                ShowInitMenu = false;
            }
        }
    }
}