- Home /
Check if two strings are the same
i'm making a system where you have to enter a code to go to a specific level, but i can't seem to figure out how to check if the string filled in in the textfield is the same as the "correct code" variable. Here's my current script:
var stringToEdit : String = "Hello World";
private var positL = Screen.width / 2;
private var PositT = Screen.height / 2;
function OnGUI () {
// Make a text field that modifies stringToEdit.
stringToEdit = GUI.TextField (Rect (positL, PositT, 200, 20), stringToEdit, 25);
}
function Update ()
{
if(stringToEdit == 4040 && Input.GetKey("Space")){
Application.LoadLevel (1);
}
}
Answer by syclamoth · Sep 20, 2011 at 11:49 AM
Ah- the issue is that GUI.TextField steals all of your keyboard inputs, so the Input.GetKey doesn't trigger because it gets interpreted as a space in your text box! Try using a GUI.Button next to the text field, instead- or set
Input.eatKeyPressOnTextFieldFocus = false;
to disable the 'input-eating' function temporarily.
And to fix the actual problem you have to compare the string with a string:
if(stringToEdit == "4040")
Answer by HowarthStudios · Jul 17, 2016 at 12:44 PM
This works perfect, I've also added a "START" button for you. Centred all GUI, and removed the Update function as it was not needed.
@script ExecuteInEditMode() // makes GUI show in edit mode
var stringToEdit : String = "Hello World";
private var positL = Screen.width / 2-100;
private var PositT = Screen.height / 2;
function OnGUI ()
{
stringToEdit = GUI.TextField (Rect (positL, PositT, 200, 25), stringToEdit, 25);
stringToEdit = stringToEdit.ToString();
if(GUI.Button(Rect(positL+50, PositT+30, 100, 25), "START"))
{
if(stringToEdit == "4040")
{
Application.LoadLevel(1);
}
}
}