If someone could tell me what I'm doing wrong I'd really appreciate it!
Basically I have a word puzzle. The player can pick from a list of letters then submit them to see if it matches a word on the board or not. This is what happens before we get to this code:
- Player clicks button with a letter attached (type of char)
- the char gets sent to this script and added to a list
- As the player submits more letters the char list gets bigger
- I'm checking if the letters equal a word in another list
If the list of chars match to a string in the "wordsToFind" list, then I want to remove the letters from said list and place the word into another list called "foundWords"....Repeating this process until player finds all words and beats the level. Everthing seemed to be working just fine until I started deleting the submitted letters.... From the inspector everything looks great, I watch the letters get added, I see it get matched up to the word, the letters are then removed and so is the word. Havent seen this type of error before so sorry if this is simple. Here is the code in question:
private const int maxLetters = 6;
public List<char> submittedLetters = new List<char>(maxLetters);
public List<string> wordsToFind = new List<string>();
public List<string> wordsFound = new List<string>();
/// <summary>
/// Update is called once per frame
/// </summary>
private void Update() {
ProcessLetters();
}
/// <summary>
/// Handles all letters being submitted and processes them
/// </summary>
private void ProcessLetters() {
if(submittedLetters.Count == 0) { return; }
string tempWord = null;
foreach (char letter in submittedLetters) {
if (wordsFound.Count >= 1) {
submittedLetters.Remove(letter);
}
tempWord += letter.ToString();
foreach (string word in wordsToFind) {
if (tempWord == word.ToUpper()) {
wordsFound.Add(tempWord);
wordsToFind.Remove(tempWord);
}
}
}
}