- Home /
how to check GUI.TextField Entry
Right, basic question. Don't normally ask questions like this but don't know where to start.
So i know how to make a text field that can be filled in. Simple stuff.
I am making a code entry. So someone finds a code in the level and has to input it in order to proceed.
What do i need to look into in order to check if what has been entered into the text field is correct.
Not asking anyone to do it for me, just want to know what i have to look at in the script reference. But any code to get me kicked off would be appriciated
Answer by TheFrankman123 · Apr 12, 2012 at 09:34 PM
I figured this out. My problem was that you need to check if stringToEding is the value inside the update not the function itself. Was me being stupid. So it would work as follows:
private var stringToEdit : String = "Enter Code";
function OnGUI () {
if(enableGUI == true) {
stringToEdit = GUI.TextField (Rect (10, 10, 200, 20), stringToEdit, 25);
}
}
function Update () {
if(stringToEdit == "1234") {
Debug.Log("code correct");
}
}
Answer by davedx · Apr 12, 2012 at 06:44 PM
Here is the docs for TextField: http://unity3d.com/support/documentation/ScriptReference/GUI.TextField.html
There's an example that shows you how to use it. Basically, all of the Unity GUI's operate in an 'immediate mode' - you call the controls as a static function and immediately receive whatever input they've received as a return value. You should put them all in your OnGUI() function (or functions called from it). Note: OnGUI() is called more than once every frame.
Good luck!
I already have the text field. What i need to do is when you click submit it stores what has been submitted so i can then check if that string = 1234 or whatever i decide the code to be.
So i realise i will have to do something along the lines of:
var stringToEdit : String = "Enter Code"; private var code : String = "1234";
function OnGUI () {
stringToEdit = GUI.TextField (Rect (10, 10, 200, 20), stringToEdit, 25);
if(GUI.Button(Rect(50,0,60,30),"Submit")) {
//store as a variable
//check to see if that varible = code.
}
}
Not sure what you're asking. If you want to know what value has been entered by the user, it's stored in stringToEdit
.
GUI.TextField()
takes a string argument (text in) and returns a string (text out). If you use the same variable to pass value and store return, it'll basically give you real-time updates. Only problem is if you want to store that value and disallow further changes to it.
I just want to check if what's been written in the text field is equal to what i want to set the code as.
if i do : if(stringToEdit == code){ or if i do : if(stringToEdit == "1234") {
Then asked it to debug log code correct if this is what's entered. But that doesn't work.
Sorry if this is getting frustrating, but can't really get my head around it.
Your answer
Follow this Question
Related Questions
TextField Query 1 Answer
GUI set max amount of characters for Label 1 Answer
Changing variables in real time. 2 Answers
TextField moves/jumps when focused 0 Answers
Decreasing Length of a String? 2 Answers