x


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);
     }
 }
more ▼

asked Sep 20, 2011 at 11:41 AM

Raymond 2 gravatar image

Raymond 2
149 107 77 86

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Sep 20, 2011 at 11:49 AM

syclamoth gravatar image

syclamoth
18.4k 20 28 118

And to fix the actual problem you have to compare the string with a string:

 if(stringToEdit == "4040")
Sep 20, 2011 at 12:31 PM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x674
x258
x155
x38
x16

asked: Sep 20, 2011 at 11:41 AM

Seen: 2168 times

Last Updated: Sep 20, 2011 at 12:31 PM