Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by sid4 · Oct 30, 2016 at 12:36 PM · else if

IF statement criteria?

im looking for the a specific filter criteria of any code word thats starts with the letters "ap" .... for example, when I added the new criteria I developed that's when the script stopped working, its definitely with the if statement I altered on my own in the script I tied adding a criteria for any word that starts with "ap" im looking at the tuts on unity but I haven't seen anything on limiting /checking answers against a specific criteria, / code list... for example lets say player reaches door 1 and the question comes up enter a code word that starts with "ap"... the answer would be apex3. or ape45, or apot7, but how do I code it so that the check runs and checks against any code that starts with the letters ap. and since there would be a few words that start with ap any would be accepted and allowed as the answer. ? I have found other things in the tuts but nothing on how to check question answers against specific criteria like this . I am getting error on my if statement. line I tried to put the if so that is any answer starts with "ap" it will be correct.

I haven't found anything in the tuts regarding asking specific criteria like the mentioned above

using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.IO;

 public class CodeWord
 {
     public HashSet<string> hash;
 }
 
 public class werda3 :  MonoBehaviour
 {
     public InputField inputField; // set this as an InputField not a GameObject
     public string wordsFile;
 
     // this will allow you to change the number of correct inputs needed (mainly for testing/debugging)
     [Range(1, 20)]
     public int correctEntriesRequired = 10; // default to 10
 
     private List<CodeWord> codeWords = new List<CodeWord>(); // create a list of CodeWords - Hashsets
     private int currentWord = 0;
 
     void Start()
     {
         for (int i = 0; i < correctEntriesRequired; i++)
         {
             // wordsFile = "C:\\Users\\new\\Pictures\\words2.txt";  // this location should be changed to a folder at the root of the drive
             // wordsFile will be words1.txt -  words10.txt
             wordsFile = "C:\\Users\\new\\Desktop\\CodeWords\\words" + (i + 1).ToString() + ".txt";
             string[] array = File.ReadAllLines( wordsFile );     // This will load a text file into an array of strings
             Debug.Log("entries in array: " + array.Length);
 
             CodeWord codeWord = new CodeWord();
 
             codeWord.hash = new HashSet<string>(array); // this converts the string array to a HashSet, could also use a dictionary for slightly better speed
             codeWords.Add(codeWord);
             Debug.Log("entries in hash for " + wordsFile + ": " + codeWord.hash.Count);
         }
 
         // the preferred method is to set the inputField in the inspector, but if it wasn't
         if ((inputField == null) &&          // if the inputField was not set in the inspector (preferred) then try and find it
             (GameObject.Find("InputField"))) // if there is an InputField game object that can be found
         {
             inputField = GameObject.Find("InputField").GetComponent <InputField>(); //This is where you assign the input field object so you can access your inputField component
         }
 
         if (inputField != null)
         {
             inputField.text = "";
             // the following adds a listener to the inputField for when the user has finished editing
             // instead of using the update method - more efficient
             inputField.onEndEdit.AddListener(delegate{LockInput(inputField);});
         }
     }
 
     // Checks if there is anything entered into the input field.
     void LockInput(InputField input)
     {
         if (input.text.Length > 0) 
         {
             if (codeWords[currentWord].hash.Contains(input.text)) // check if the HashSet contains the code they entered 
             {
                 currentWord++;
                 input.text = "";
                 if (currentWord >= correctEntriesRequired)
                 {
                     // all entries were correct now do something - for example
                     AllCodewordsCorrect();
                 }
             }
             else
             {
                 // do something if not correct - for example
                 currentWord = 0; // force restart
                 Destroy(gameObject, 1); // magic door closes - remove object
             }
         }
         else if (input.text.Length == 0) 
         {
             Debug.Log("Main Input Empty");
         }
     }
 
     void AllCodewordsCorrect()
     {
         // do something here after all entered code words are deemed valid
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by oStaiko · Oct 30, 2016 at 03:09 PM

I'm not sure where in your code you're trying to do that, so I'll just go over how to do it in general. First off I'd reccomend only typing "Unity" along with your searches if it's actually relevant to Unity. Otherwise if it's for coding help, just type c# and in my experience you'll get a lot better help. For example what you're looking for is the String class methods which can be found at [Microsofts c# documentation][1].

If you want to test if a string starts with specific characters:

 string input = "Hello": //Users string
 string answer "He"; //First two letters to check for
 
 if ( input.StartsWith( answer ))
 {
     //Pass
 }
 else
 {
     //Fail
 }

You can also make strings more readable with .ToLower() and .Trim(). These change all letters to lowercase and remove any spaces/tabs/new lines at the beginning and end of the strings. Otherwise, "hello" != "Hello" != " Hello ".

 input.ToLower().Trim();
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 sid4 · Oct 30, 2016 at 04:23 PM 0
Share

its not working heres what I used using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.IO;

     public class CodeWord
     {
         public HashSet<string> hash;
     }
     public string input = "Hello": //Users string
     public class werda4 :  $$anonymous$$onoBehaviour
     {
         public InputField inputField; 
         public string wordsFile;
         
         [Range(1, 20)]
         public int correctEntriesRequired = 10; // default to 10
 
         private List<CodeWord> codeWords = new List<CodeWord>();
         private int currentWord = 0;
 
         void Start()
         {
             for (int i = 0; i < correctEntriesRequired; i++)
             {
                 // wordsFile = "C:\\Users\\new\\Pictures\\words2.txt";  // this location should be changed to a folder at the root of the drive
                 // wordsFile will be words1.txt -  words10.txt
     wordsFile = "C:\\Users\\new\\Desktop\\CodeWords\\words" + (i + 1).ToString() + ".txt";
 string[] array = File.ReadAllLines( wordsFile );  // This will load a text file into an array of strings
                 Debug.Log("entries in array: " + array.Length);
 
                 CodeWord codeWord = new CodeWord();
 
 codeWord.hash = new HashSet<string>(array); // this converts the string array to a HashSet, could also use a dictionary for slightly better speed
                 codeWords.Add(codeWord);
                 Debug.Log("entries in hash for " + wordsFile + ": " + codeWord.hash.Count);
             }
             if ((inputField == null) &&          // if the inputField was not set in the inspector (preferred) then try and find it
                 (GameObject.Find("InputField"))) // if there is an InputField game object that can be found
             {
                 inputField = GameObject.Find("InputField").GetComponent <InputField>(); //This is where you assign the input field object so you can access your inputField component
             }
 
             if (inputField != null)
             {
                 inputField.text = "";
                 // the following adds a listener to the inputField for when the user has finished editing
                 // ins$$anonymous$$d of using the update method - more efficient
                 inputField.onEndEdit.AddListener(delegate{LockInput(inputField);});
             }
         }
         void LockInput(InputField input)
         {
             if (input.text.Length > 0) 
             {
             if ( input.StartsWith( "ap" )) + (codeWords[currentWord].hash.Contains(input.text)) // check if the HashSet contains the code they entered 
                 {
                     currentWord++;
                     input.text = "";
                     if (currentWord >= correctEntriesRequired)
                     {  
                         AllCodewordsCorrect();
                     }
                 }
                 else
                 {   
                     currentWord = 0; // force restart
                     Destroy(gameObject, 1); // magic door closes - remove object
                 }
             }
             else if (input.text.Length == 0) 
             {
                 Debug.Log("$$anonymous$$ain Input Empty");
             }
         }
         void AllCodewordsCorrect()
         {     // do something here after all entered code words are deemed valid
         }
     }
avatar image oStaiko · Oct 31, 2016 at 02:04 AM 0
Share
 if ( input.StartsWith( "ap" )) + (codeWords[currentWord].hash.Contains(input.text))

This is incorrect syntax for an if statement and might be whats giving you errors. The proper systax would be:

 if (input.StartsWith( "ap" ) && codeWords[currentWord].hash.Contains(input.text))
 {
     //Stuff
 }
avatar image sid4 oStaiko · Oct 31, 2016 at 11:28 AM 0
Share

im still getting these errors its now in the beginning of the code

I think where I added your code is misplaced

Assets/dart/werda4.cs(7,22): error CS0101: The namespace global::' already contains a definition for CodeWord'

Assets/dart/werda4.cs(12,32): error CS8025: Parsing error

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

77 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

If/elseif code doesn't seem to work 1 Answer

If statements with AudioListener C# ? 0 Answers

Add sprint Key 2 Answers

Looking to overlook arguments based on parameters elsewhere in the code. (C#) 0 Answers

Flipping Switches 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