- Home /
Syntax, how do I limit unityscript from jumping to the end of my code?
so, I've created this code:
var firstTextField : String = "Type Your Answer Here";
var speakerText : String = "This is the first task; can you 'continue'?";
var correctAnswer : String = "continue";
function OnGUI () {
//make a label x, y, width, height
GUI.Label (Rect (Screen.width/2-100, Screen.height/2-70, 200, 100), "" + speakerText);
// Make a text field that modifies stringToEdit.
firstTextField = GUI.TextField (Rect (Screen.width/2-150, Screen.height/2-10, 300, 20), firstTextField, 25);
if (Input.GetKeyDown ("return") && firstTextField == correctAnswer)
{
print ("enter key was pressed");
firstTextField = "";
speakerText = "Good, now can you 'hop'?";
correctAnswer = "hop";
} else if(Input.GetKeyDown ("return") && firstTextField !== correctAnswer) {
print ("enter key was pressed");
firstTextField = "";
speakerText = "try again, now can you 'continue'?";
}
if (Input.GetKeyDown ("return") && firstTextField == correctAnswer)
{
print ("enter key was pressed");
firstTextField = "";
speakerText = "Good, now can you 'jump'?";
correctAnswer = "jump";
} else if(Input.GetKeyDown ("return") && firstTextField !== correctAnswer) {
print ("enter key was pressed");
firstTextField = "";
speakerText = "try again, now can you 'hop'?";
}
}
what I want it to do is to show a "hint" then if you write the correct answer into the text field, and press enter, then the script moves on to the next "phase" by changing the "correctAnswer" variable, and the "speakerText" variable. but when I run the script, and type in the answer(or anything else for that matter) the script jumps all the way to the bottom and changes the variable "speakerText" to "try again, now can you 'hop'?".
So I'm wondering what I'm doing wrong, is it the "!=" operator I used? or is it the way I have formatted my if statements in line after each other, or is it the fact that I'm using the "function OnGUI" function; or is it something completely different? :)
Thanks again for any help! :) This must be like the fifth question or so I've posted today, and every time you guys have come through! :D
Answer by Benproductions1 · May 17, 2014 at 08:05 AM
I can't really tell what you're actually trying to accomplish, but I can show you why the result will always be "try again, now can you 'hop'?"
and you should be able to figure out how to accomplish what you want.
Lets do some desk-checking for the OnGUI
function:
The text field gets rendered with the result stored in "firstTextField"
We check whether or not the "enter" button has been pressed and whether we have the correct result in "firstTextField". Assuming we pressed the enter button, in either case, we set "firstTextField" back to
""
.Then we check again for the "enter" button, which must be true, since we just assumed such.
Now we check whether "firstTextField" is the correct answer. Since we just reset it, it really can't be.
Lastly we change the "speakerText" to "try again ..."
I think you need to rethink how you are approaching the entire problem. You should also note that ever using the word "first" or a number in a variable hints towards a bad design decision. Either there is one of something, or a list/array of things. Having multiples of something is just asking for trouble.
Thank you sir! :)
first off, I learned a new word! :D desk-checking!
and, yeah.. I can see the problem there now, and I think I have an Idea on how to fix it; maybe :P
yeah, thanks, the first thing is something I didn't think of; I'm doing this as sort of a learning experience, so I called it firstTextField, because it was the first text field I ever created :P and I never thought of it that way :)
I'll clean up my code now, and hopefully it'll be working like a charm :)
Thanks again!
I tried changing my code a bit, to make a boolean value of the different things that need to be true for the script to move on. I also commented a bit on it to make it easier to see what I'm trying to accomplish..
I'll add the script in another comment to make it fit :)
now it moves to the first else if statement, and stops there displaying "try again, now can you 'continue'?"
var answerField : String = "Type Your Answer Here";
var speakerText : String = "This is the first task; can you 'continue'?";
var correctAnswer : String = "continue";
var isEnterPressed = false;
var isTheCorrectAnswerEntered = false;
function Update () {
//Check if enter is pressed, and if it isn't, change the isEnterPressed variable accordingly
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Return)){
print ("up arrow key is held down");
isEnterPressed = true;
}else{
isEnterPressed = false;
}
//make a boolean value of whether the text in the textfield is the correct answer or not
if (answerField == correctAnswer){
isTheCorrectAnswerEntered = true;
}else{
isTheCorrectAnswerEntered = false;
}
}
function OnGUI () {
//make a label x, y, width, height
GUI.Label (Rect (Screen.width/2-100, Screen.height/2-70, 200, 100), "" + speakerText);
// $$anonymous$$ake a text field that modifies answerField.
answerField = GUI.TextField (Rect (Screen.width/2-150, Screen.height/2-10, 300, 20), answerField, 25);
/*check whether both conditions; if return is pressed, and the correct answer is filled into the textfield.
then change the label to ask a new question, and to change the answer to one appropriate to the question, also change the isEnterPressed variable
to false again, to make sure it doesn't stay on true after the question has been answered; it also clears the answerField textfield, so that
it doesn't have anything in it for the next question.*/
if (isEnterPressed == true && isTheCorrectAnswerEntered == true)
{
print ("enter key was pressed");
answerField = "";
speakerText = "Good, now can you 'hop'?";
correctAnswer = "hop";
isEnterPressed = false;
/* in the else if statement I want to tell the script that if you press the return key, and you haven't filled in a statement in the textfield, then
the script should ins$$anonymous$$d give an alternate response by changing the label to a "try again" statement, leaving the answer the same. */
} else if(isEnterPressed == true && isTheCorrectAnswerEntered == false) {
print ("enter key was pressed");
answerField = "";
isEnterPressed = false;
speakerText = "try again, now can you 'continue'?";
}
if (isEnterPressed == true && isTheCorrectAnswerEntered == true)
{
print ("enter key was pressed");
answerField = "";
speakerText = "Good, now can you 'jump'?";
correctAnswer = "jump";
isEnterPressed = false;
} else if(isEnterPressed == true && isTheCorrectAnswerEntered == false) {
print ("enter key was pressed");
answerField = "";
speakerText = "try again, now can you 'hop'?";
isEnterPressed = false;
}
}
Answer by FarbrorMartin · May 17, 2014 at 06:02 PM
Your input logic looks weird.
If you limit your logic to just checking the answer when enter is pressed it'll get a lot simpler i think.
if (Input.GetKey (KeyCode.Return)){
if (answerField == correctAnswer) {
// print correct answer stuff here.
}
else {
// Print wrong answer stuff
}
}
Your answer
Follow this Question
Related Questions
If/else to case ? 1 Answer
Double If condition syntax ? 1 Answer
EXTREMELY basic scripting question. 2 Answers
Comparing text from www.downloadhandler.text to string does not work 1 Answer