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\
ew\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
ew\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
}
}