Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zzzspawn · May 17, 2014 at 01:40 AM · if-statementssyntaxoperatorformatting

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image abhishek7 · May 17, 2014 at 06:34 PM 0
Share

use c# next time

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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:

  1. The text field gets rendered with the result stored in "firstTextField"

  2. 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 "".

  3. Then we check again for the "enter" button, which must be true, since we just assumed such.

  4. Now we check whether "firstTextField" is the correct answer. Since we just reset it, it really can't be.

  5. 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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zzzspawn · May 17, 2014 at 12:52 PM 0
Share

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!

avatar image zzzspawn · May 17, 2014 at 04:04 PM 0
Share

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'?"

avatar image zzzspawn · May 17, 2014 at 04:04 PM 0
Share
 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;
     
     }
 
     
 
 }
 
 
     
     
avatar image
0

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
 }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

optional function parameters in javascript and c# 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges