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
}
}
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();
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
}
}
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
}
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
Follow this Question
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