helping in the unity

i making an war game in the unity.

i am making a mission that the subject is to writing the details that the video we

making telling you so i wanna know how make that the game know when the users

writing the right detail?

and someone write me this: In a script, in the OnGUI function, use a GUI.TextField the user can use to write stuff in, then compare the content of what he types with what you want, and that's it!

so there is something i dont understand: so how am i compare the content of what he types with what i want


In OnGUI - do this -

var ifWhatIWant : boolean = textFieldValue == "variableIwant";

remember it will be case sensitive.

That will return a boolean which you can use in a if function and for other things...



Are there any other details you need?

Hope this helps!

I found it a little hard to understand your question, so apologies if i've misunderstood. Basically it sounds like you want to know what the player typed in? Have you read the Unity GUI information about textfields in the scripting docs? You will see that the input of the textfield is stored in a variable. Knowing this, you can compare two strings simply by using '==', so:

var string1 : String;
var string2 : String;

if(string1 == string2){


I think you need to account for case ('hello' being different from 'Hello'). A quick search of the forums suggests you can either:

  • Use the .Net method String.ToLower() to change the string's case to lower case before the comparison

  • Use the .Net method String.Compare(String1,String2,Boolean) to compare the strings where the boolean determines whether you ignore case or not:

    string str1 = "BLAH";

    string str2 = “blah”;

    bool sameString = String.Compare(str1, str2, true);

If you are interested in parts of a string, again a quick forum search suggests you can use IndexOf:

if ( longString.IndexOf(subString) > -1 ) { 

I have not used these myself but this is just what I found in two minutes of searching. Anyone with more experience, feel free to correct me.