- Home /
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.
Answer by Skjalg · Mar 17, 2011 at 03:05 PM
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; }; };
Awesome thanks heaps :D also..is there anyway to freeze the screen from moving until you click submit? i tried Screen.lockcursor = true and it did nothing >> i need to stop the screen from moving while that form is active.
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
Answer by yoyo · Mar 17, 2011 at 03:03 PM
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;
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613113405im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
HighScore Names 1 Answer
Set name for game 0 Answers
In-game Player username 1 Answer
how to put the name of a car ??? 0 Answers