- Home /
Question by
WanAmir · Jul 18, 2021 at 11:25 PM ·
programmingfor-loop
for loop not working properly
This is my code:
List<Word> spawnedWords = new List<Word>();
public void CheckWord()
{
inputText = inputText.ToLower().Trim();
print(spawnedWords.Count);
for (int i =0; i < spawnedWords.Count; i++)
{
print(i);
if (inputText == spawnedWords[i].selectedWord)
{
spawnedWords[i].TakeDamage();
}
}
inputText = string.Empty
}
I link CheckWord() method to the Enter button and when I pressed it, the for loop didn't complete all the loop. Only some of spawnedWords execute OnTakeDamage(). Let say I spawned 5 Word objects, print(spawnedWords.Count) showed 5 but print(i) showed only 0,1,2. Wht is this happening? Please help~
Comment
Probably because only some of your words satisfy your if statement, put a print statement above your if block on what spawnedWords[i].selectedWord is.
Your answer
![](https://koobas.hobune.stream/wayback/20220613040511im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Using i in if statement 3 Answers
Question on Possible FOR Loop Condition Conflict 0 Answers
For Loop is Only doing it's Job Once 2 Answers
Yet Another Bullet Decal Problem 0 Answers