- Home /
Text input command terminal
var visible;
var controlTexture : Texture2D;
var gameCode : String = "1234";
var stringToEdit : String;
var buttonMessage : String = "Incorrect Command";
function OnGUI(){
if( visible )
{
stringToEdit = GUI.TextField (Rect (20, 100, 70, 25), stringToEdit, 4);
if(stringToEdit == gameCode){
buttonMessage = "Begin Game";
}else{
buttonMessage = "Incorrect Command";
}
if(GUI.Button(Rect(95, 100, 95, 35), buttonMessage) && stringToEdit == gameCode){
Application.LoadLevel ("1");
}
}
}
function Update(){
if(Input.GetKeyDown(KeyCode.M))
{
visible = !visible;
}
}
I am trying to set up a pseudo-computer interface / command terminal (like the computer in black ops) but am unsure of how to make the text input load a level and even when the stringToEdit is equal to the gameCode, nothing happens. Could someone help me or at least guide me in the right direction. Thanks in advance
Answer by ckfinite · Jun 27, 2011 at 01:03 PM
Try Application.LoadLevel(1);
thanks, I think the problem is with var gameCode : String = "1234"; because if ins$$anonymous$$d of gameCode in
if(stringToEdit == gameCode){
I put 1234, it works
Okay, I think you have already tried these, but I will go through them, just so I am in the same place as you are.
-Does anything modify gameCode?
-If you log gameCode, is it what you expect it to be?
-Have you tried trim$$anonymous$$g whitespace off the input string?
To go along with @Bunny83's answer, you did copy my code exactly, right? The code I posted uses a int, where the code you have now uses a string. Also, it works fine in my test project too.
I think I found the problem, my version of Unity can not set numbers as String - I set a word as String using the exact same code posted here and it worked.
Numbers and characters are represented exactly the same in the string data structure. Are you using the numeric keypad for entering numbers?
Answer by Bunny83 · Jun 27, 2011 at 01:13 PM
I'm not sure what's your actual problem, because it works in my test project...
Just make sure that the level you want to load exists. If you use a string to load a level it have to match the level name.
If you use an int it's the level index that is specified in the buildsettings