Making multiple if / else statements?
this code checks a word input against the list "words" . .. but how do I add multiple different if statements one rite after the other, so user must get 10 code words correct ? each correct word will allow user to next code word, and until he reaches action will be taken , door to player opens etc whatever.... this code works with one check the "words" list I have more statements needed
so when user inputs correct code an action is taken but what if I wanted to add another set of code after the first code word, so multiple code words . each word checked from different lists if player types correct 1st word it then goes to the 2nd list and checks for the next typed input word,
so for example I have 10 lists each test with different code words
and after the 1st correct word is typed user then must input the next codeword and then the next etc so 10 lists to pull from and so 10 codewords
I thought about pasting the code after that code in same script since its in the same input field box but I think its wiser to just add more if /else statements
correct?
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class werda : MonoBehaviour
{
public GameObject inputField;
public string wordsFile;
private HashSet<string> hash;
void Start()
{
wordsFile = "C:\\Users\\new\\Pictures\\words2.txt";
//wordsFile = Application.dataPath + "/words2.txt";
inputField = GameObject.Find("InputField"); //This is where you assign the input field object so you can access your inputField component
inputField.GetComponent <InputField>().text = ""; //This makes it so the input field starts with nothing in it.
string[] array = File.ReadAllLines// This will load a text file into an array of strings
Debug.Log("entries in array: " array.Length);
hash = new HashSet < > (array); // this converts the string array to a HashSet, could also use a dictionary for slightly better speed
Debug.Log("entries in hash: " hash.Count);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.))
{
string input = inputField.GetComponent < > ().text; // set a string to be what they entered
Debug.Log("checking word: "input);
if (hash.Contains()) // check if the HashSet contains the code they entered
{
Debug.Log("word found");
// If they get here the code is correct magic door opens
// i.e its in the list of 60,000 words
// do SOMETHING
} else {
//Debug.Log("word NOT found");
Destroy (gameObject, 1 );
// IF they get here... then they entered a WRONG CODe , magic door closes
// i.e not in the list of 60k words
// DO SOMETHING
}
}
}
}
Answer by TBruce · Oct 26, 2016 at 06:26 PM
I do not understand what you mean when you say that you have 10 lists
. You have a single HashSet
private HashSet<string> hash;
Do you want to use just the hash defined or will you have multiple ones (10 of them)?
Here is how you can achive what you want easily just by using the single HashSet
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class werda : 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 HashSet<string> hash;
private int currentWord = 0;
void Start()
{
wordsFile = "C:\\Users\\new\\Pictures\\words2.txt"; // this location should be changed to a folder at the root of the drive
string[] array = File.ReadAllLines( wordsFile ); // This will load a text file into an array of strings
Debug.Log("entries in array: " + array.Length);
hash = new HashSet<string>(array); // this converts the string array to a HashSet, could also use a dictionary for slightly better speed
Debug.Log("entries in hash: " + 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 (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
}
}
yes u are correct in this script there is only 1 list (hashset) but i have other lists om my desktop I will import but i would need to code first and then fill in the blanks where the other 9 lists need to be referred to in the script.. yes multiple lists the reason for this is so user cannot enter any of the codes in any order for example lets say 5 of the code words are run31, walk2 sun33 can3 , hot6, when the first question appears it will ask for a certain code which is run31, then if user gets correct he will have to input the second question , the answer to 2nd q is walk2 and etc. etc. so the order of answers is depending on the question asked , if all words were included in same list then user could enter any of the codewords in any order and that is not what im looking for, I want when a certain code is asked to be input the user will have to type that correct code only, (please note, each question will include a hint at what the answer is so there will be an idea of what code to type) that is why I thought using many lists is the only way but again if its easier to use 1 list ok ill accept that then,, it is definitely possible to put all words in the 1 list, that shouldn't be a problem, im just worried it will accept any word form the 1 list if all are put in the one.
(please note, if possible to accomplish this in one list that's ok but I think it is easier to use many lists to refer to) but again im ok with what ever is easier and more optimal on the system.
You have everything here you need then. All that you need to do is supply the other lists. You can do something like this.
when u say in the code = " this location should be changed to a folder at the root of the drive" do u mean make 1 master folder and keep all 10 lists in the master folder and then make the link reference in the script to the master folder only?
When I wrote
this location should be changed to a folder at the root of the drive
I meant that you do not want the file(s) under C:\Users
. The example I used in my updated code was just that, an example. The optimum place to put the files would be in a folder under the Assets
folder but place it anywhere.
The reason I say place it somewhere under the Assets folder is it is more secure (also you are limiting what platforms that you can make your game for). Personally I like Assets\Resources\Data
. I have a folder called _ProjectSkeleton
. In that folder I have the following folders that I copy to every new project (I can delete what I do not need)
Assets\_Scenes
Assets\Editor
Assets\Resources
Assets\Resources\Animations
Assets\Resources\Audio
Assets\Resources\Data
Assets\Resources\Fonts
Assets\Resources\$$anonymous$$aterials
Assets\Resources\$$anonymous$$odels
Assets\Resources\Prefabs
Assets\Resources\Textures
Assets\Scripts
Lets say you have 10 files
words1.txt
words2.txt
words3.txt
words4.txt
words5.txt
words6.txt
words7.txt
words8.txt
words9.txt
words10.txt
and you placed them in a folder called CodeWords
at the root of drive c
, the path of each file would look like this
c:\CodeWords\words1.txt
c:\CodeWords\words2.txt
c:\CodeWords\words3.txt
c:\CodeWords\words4.txt
c:\CodeWords\words5.txt
c:\CodeWords\words6.txt
c:\CodeWords\words7.txt
c:\CodeWords\words8.txt
c:\CodeWords\words9.txt
c:\CodeWords\words10.txt
and the code would look like this
c:\\CodeWords\\words1.txt
c:\\CodeWords\\words2.txt
c:\\CodeWords\\words3.txt
c:\\CodeWords\\words4.txt
c:\\CodeWords\\words5.txt
c:\\CodeWords\\words6.txt
c:\\CodeWords\\words7.txt
c:\\CodeWords\\words8.txt
c:\\CodeWords\\words9.txt
c:\\CodeWords\\words10.txt
Now since this is being done in a loop, all 10 file paths do not need to be hard coded. You will build it ins$$anonymous$$d like this
for (int i = 0; i < correctEntriesRequired; i++)
{
wordsFile = "C:\\CodeWords\\words" + (i + 1).ToString() + ".txt";
// do everything else here - load the file, add to hashset, add hashset to list
}
I will test in morning and let u know how it works out
thank u
Your original code is using an InputField. I suggest first looking at the docs on the InputField and then looking at some online tutorials.
As I said, you need to read the docs and go thru the tutorials.
As long as you have your UI setup in Unity the code above will work perfectly for you (which is what was assumed in the first place because of the original question). Go to the online tutorials link that I gave you if you do not know how to set up an InputField.
What you need to help you accomplish your goal is already available to you on the internet both in the way of as tutorials and in the answer that I provided to your question. Please do not take advantage of the people that donate their time to help out by answering questions.
Another thing is that you should Break multi-part problems into simpler questions
- taken from the FAQ.
Give a $$anonymous$$an a Fish, and You Feed Him for a Day. Teach a $$anonymous$$an To Fish, and You Feed Him for a Lifetime
im looking at the tuts u send now theres a lot -im going thru them all.I already found something's out but I haven't seen anything on limiting /checking answers against a 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.
The code I provided does everything. You just need to supply the interface. To do this
Create your new project
Add an
InputField
.Either create a game object separate from the canvas and add my version of the werda script to it or add my version of the werda script to the canvas.
From the inspector click on the object (game object created or canvas) that has the werda script attached to it.
Now drag the
InputField
from the inspector to the InputField variable (ins$$anonymous$$d of dragging you can select it.Now, the only other thing you need to do is make sure of is that you have your file(s) in the proper location as coded in the werda script.
Now, since you are using multiple files you could do what I suggested above and place all files in one folder. Then have a list of TextAsset values. A TextAsset allows you to drag and drop a file into the inspector. The data is immediately read from the file at runtime.
Here is an updated version of werda.cs. This is what it entails
Filenames are no longer embedded in the code. Ins$$anonymous$$d there is a list of TextAssets which allows the list to be set from the inspector. This also allows you to place all code files in a single folder somewhere underneath the assets folder.
Files are no longer manually loaded in code. This is a feature of the TextAssets itself.
Because a list of TextAssets's are used, the hashsets is easily created in a loop.
Added dynamic update of the PlaceHolder prompt using the variable
placeHolderPrompt
in the werda script. Defaults to "Enter code word".
You have everything you need to accomplish your goal. Please remember this is a site to help answer peoples questions about Unity. This is not a site to $$anonymous$$ch people how to program. Please read the FAQ and users guide. The FAQ states this
Some reasons for getting a post rejected: - Asking us to fix your code: more than likely, you simply need to get a better understanding of basic program$$anonymous$$g. Having a look at the Scripting tutorials on the Learn page will help you become more familiar with scripting in Unity
hi thanks but I had the script running fine . the input field was already attached and all working , but 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 u send me 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 how do I code it so that the check runs and checks against any code that starts with the . and since there would be a few words that start any would be accepted and allowed as the answer. ? I am getting error on my if statement. line I tried to put the if so that is any answer starts with "" it will be correct.
but again the ui I am knowledgeable with , its the code that's hard for me to find , I haven't found anything in the tuts regarding asking specific criteria like the mentioned above
----I had to cut this code short it wouldn't fit on this comment page. ---
void Update()
{
if (ap.StartsWith(""))
(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.))
30. {
string input = inputField.GetComponent < ().text; // set a string to be what they entered
Debug.Log("checking word: " + );
if (hash.Contains()) // check if the HashSet contains the code they entered
35. {
Debug.Log("word found");
// If they get here the code is correct magic door opens
// i.e its in the list of 60,000 words
// do SO$$anonymous$$ETHING
40. } else {
//Debug.Log("word NOT found");
Destroy (gameObject1 );
// IF they get here... then they entered a WRONG CODe , magic door closes
// i.e not in the list of 60k words
45. // DO SO$$anonymous$$ETHING
}
}
}
}
you are correct sorry I did use what you said but im getting overwhelmed with this coding its very tough yes organlly I wanted the if else and yes it works , sorry its just im trying to accomplish ths codeing and it is very hard = every little thing must be coded, every thing, so I lose track of time and things
If you are overwhelmed then I suggest you try and do one thing at a time. I see all the different unrelated questions that you are asking on UA. You need to finish one thing at a time.
ye s but a lot of my past questions were never answered and I had to repost at times
so I think its much more productive to put all questions that one is ready to attack out there as long as he will put the info to use I know a lot are somewhat unrelated but sometimes if you get stuck on 1 thing it will free your $$anonymous$$d to attack another area to keep things moving
Please read the answer to this question in its entirety.
I hate to disagree with you but it is not more productive to ask multiple unrelated questions. It is O$$anonymous$$ if this your job to handle multiple things at once (but that would mean that you know how to accomplish your task in the first place because that is what you were hired to do).
You are asking multiple questions and this is what professional programmers and moderators here see
If they have answered a question that you asked previously but you keep dragging it out because you do not understand the answer, it is not what you were looking for (e.g. I rewrote your class properly and did not add to it) or you want to ask more questions. You do not mark the question as answered in a timely manner.
$$anonymous$$oderators and people that devote their free time to people with questions can see your history. They can see the frequency of answers that were never accepted. They can see that you ask to many questions when your previously answered ones were not accepted. They can see that you ask the same question multiple times (As I said, you may think that it is a good thing but it is not).
If you read the FAQ and users guide as I previously suggested you will see that a lot of what I have already said is true. You will also see reasons for questions being thrown out. $$anonymous$$oderators here have been nice and they have not done this, ins$$anonymous$$d your questions have just been ignored.
As I mentioned above. Accept my answer and I will provide you with a working package. I have finished with this question.
I understand ur point of view but think about it like this
in school students learn many subjects not just one, and there is a reason for this,
I do understand what you mean but also think about this, its not illegal (in unity terms) to ask unrelated questions, everyone learns at their own pace , as long as you are trying your very best to learn and when u get stuck ask a question I see no harm or bad ethical side of asking unrelated questions, again only if they are being learned from. And not being asked just for the hell of it, if they are genuinely being a learning tool and helpful I see it ok.
Your answer
Follow this Question
Related Questions
mathf.abs(0) is higher than 90, why? 1 Answer
Help with rotation please 2 Answers
problem with lists 0 Answers
[Solved] Error in this code? 2 Answers
Infinite Time inbetween if statements? 2 Answers