- Home /
TextField Query
Hi there,
I've got the following script
#pragma strict
var stringToEdit : String = "Type Code Here";
function OnGUI(){
stringToEdit = GUI.TextField (Rect (10, 10, 200, 20), stringToEdit, 25);;
if (GUI.Button (Rect(Screen.width/2,Screen.height/2,80,20), "Submit")){
if(stringToEdit == "LCD TV"){
Application.LoadLevel ("TVscene");
}
if(stringToEdit == "Camera"){
Application.LoadLevel ("Camerascene");
}
}
}
Which works great. However, if the user types in something in the textfield that isn't specified in the code, I want some sort of error message to come up (i.e. "Error: Invalid Keyword"). Could I achieve this with an else statement perhaps?
Anyone have any ideas?
Thanks
Answer by AlfredDuler · Sep 10, 2013 at 03:36 PM
var StringIfError : String = "";
function OnGUI()
{
stringToEdit = GUI.TextField (Rect (10, 10, 200, 20), stringToEdit, 25);
StringIfError = "";
if(stringToEdit != "LCD TV" && stringToEdit != "Camera")
{
StringIfError = "Error: Invalid Keyword";
}
if (GUI.Button (Rect(Screen.width/2,Screen.height/2,80,20), "Submit"))
{
if(stringToEdit == "LCD TV")
{
Application.LoadLevel ("TVscene");
}
if(stringToEdit == "Camera")
{
Application.LoadLevel ("Camerascene");
}
}
GUI.Label(Rect(10, 30, 100, 20), StringIfError );
}
Yeah, that doesn't seem to work, it does nothing. It's meant to be that when I type anything other than "LCD TV" or "Camera" it comes up with the error message
The OnGUI function is an update function. In my first answer, the Label with the error was only displayed the frame you clicked the button, so something like 1/200 seconds = Invisible;
So in the new answer: you set a label to display an empty string, so it isn't visible, until the keword don't match then it's set to "Error..."
Great, it works! Thank you for the help. Unfortunately I can't accept your answer as correct because its a comment!
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
how to check GUI.TextField Entry 2 Answers
Why do I have to call ToString() when fetching a String from another script? 2 Answers
Decreasing Length of a String? 2 Answers
How to tell if a string contains some specified text 6 Answers