- Home /
my foreach loop and condition wont work
i created a for each loop to remove 2 data on my list but when i want it back i cant add it anymore okay here's the code, I think the code will explain it better than me
private List<string[]> questions = new List<string[]>();
public List<int> answerOrder = new List<int>(new int[] {1,2,3,4});
void OnGUI() {
if (remove){
foreach(int item in answerOrder){
answerOrder.Remove(2);
answerOrder.Remove(3);
print (item);
}
}
if (GUI.Button(new Rect(300, 250, 100, 30),"50/50")) {
remove=true;
}
private void HandleAnswer(int answer) {
if (answer == 1) {
NextQuestion();
do{
foreach(int item in answerOrder){
answerOrder.Add(2);
answerOrder.Add(3);
}
}while(remove = true);
}
else {
}
}
Comment
while(remove = true); ??? Does that even compile? did you mean: while(remove == true) { //dostuff? }
You can't remove items from a list which you're enumerating. Think about it, you're stating, FOREACH item/entry in answerOrder...remove(x), that confuses the foreach statement because you changed the quantity of the items while going through the items.
Also if you're remove by index, what is the point of the foreach except the debug statement.