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 meaningoflife42 · Jul 20, 2014 at 02:18 AM · javascriptbug

Something wrong with or operator

Hello, as stated in one of my previous questions, I am in the process of making a text adventure. Halfway through my coding session, the or operator suddenly stopped working. #pragma strict

 var arcadeText : GUIStyle;
 var stringToEdit : String = "";
 private var showBox1 : boolean = true;
     function OnGUI () {
         // Make a text field that modifies stringToEdit.
         var returnHit = (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return);
         stringToEdit = GUI.TextField (Rect (19, 397, 600, 20), stringToEdit, 240, arcadeText);
     
          if(returnHit) {
 Animate();
 
     
     }
     if (showBox1 == true)
     
    {GUI.Box(Rect(0,0,Screen.width,Screen.height),"Greetings, Adventurer. You have but one goal: to return to reality.\nYou are in a low-lit stone room with a single unlocked door visible.", arcadeText);}
    var examineRoom1 = returnHit && stringToEdit == ("examine" || "Examine" ||"examine room" ||"Examine room"||"check room" ||"Check room" || "look" ||"Look" ||"look around" ||"Look around"); 
    if (examineRoom1) {showBox1 = false;}  
      
         }
 function Animate () { stringToEdit = stringToEdit; yield WaitForEndOfFrame; stringToEdit= "";} 

Specifically, in the line regarding defining examineRoom1 and then the next if then statement, any other word besides examine (the first and only the first works) is completely disregarded and does not work at all, even if it is on the list of approved words. This just started occurring after I tried to help my future self by making a variable defining all examine words so I would not have to copy and paste the list in any future rooms the text adventure would possess. When I made this variable and made the adjustments to the code so that instead of that massive text block, it just would say && stringToEdit ==(examine1), the name of the variable I chose. From then on, even after I reversed the original changes, all strings preceding the first would be disregarded. Any help would be appreciated in fixing this game breaking bug.

Comment
Add comment
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

2 Replies

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

Answer by Kiwasi · Jul 20, 2014 at 03:26 AM

I'm surprised it ever worked like that. Here is an alternative way to structure it. An array will be much nicer to work with in general.

 // Put all of the possible strings in here
 private string[] words;

 private bool IsStringOnList (string wordToTest){
     for each (string word in words){
         if(word == wordToTest){
             return true;
         }
     }
     return false;
 }

Comment
Add comment · Show 2 · 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 Lo0NuhtiK · Jul 20, 2014 at 04:09 AM 0
Share

if(word == wordToTest) @Bored$$anonymous$$ormon's typo on line 6

avatar image Kiwasi · Jul 20, 2014 at 05:26 AM 0
Share

Fixed. Thanks, that one always catches me out when I'm making up code on the fly. Have fixed it now.

I think C# also has a contains or any method that could do the same thing.

avatar image
0

Answer by meaningoflife42 · Jul 21, 2014 at 02:32 AM

Fixed your code a tad, final version:

 private var examineWords= ["examine", "Examine","examine room","Examine room","check room","Check room", "look","Look","Look around","Look around"];
  var wordToTest:String;
 private var IsStringOnList : boolean;
 {for each (var word in examineWords){if (word == wordToTest){ IsStringOnList=true;}}
 if (IsStringOnList == true){ Debug.Log("Success");}}

Thanks for the help!

Comment
Add comment · Show 1 · 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 Kiwasi · Jul 21, 2014 at 02:51 AM 1
Share

This might be the first time I've seen any benefit to JavaScript and weak typing.

Still don't like it. :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

unity bug??? while loop is satisfied, but doesn't quit 2 Answers

Keyboard Cursor Delay Issue 0 Answers

Text clearing issue- game breaking 1 Answer

semicolon expected while semicolon is present 1 Answer

Android port bug? 1 Answer


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