List, for loop and removal of items
Hey there, first time writing here, so bare with me if I'm doing something wrong.
So what I'm currently working on is a game for parties. Now, I made a Settings scene containing 3 checkboxes, Questions, Games and Dares, and you can toggle/untoggle them.
Then when the game initializes, I create a loop through all the questions and check their categories (Which are selected in the inspector for each question) and if the questions category matches one of the checkboxes (If you toggle/untoggle, they change a public bool which I can access for each category) then the question has to be removed.
It works fairly well, the checkboxes change the bools etc. but the removal of the ones that are unselected doesn't work.
This is the loop:
for(int i = 0; i < AllQuestions.Count; i++)
{
if (AllQuestions[i].category == "Question" && !SettingsManager.Questions)
{
AllQuestions.Remove(AllQuestions[i]);
}
else if (AllQuestions[i].category == "Game" && !SettingsManager.Games)
{
AllQuestions.Remove(AllQuestions[i]);
}
else if (AllQuestions[i].category == "Dare" && !SettingsManager.Dares)
{
AllQuestions.Remove(AllQuestions[i]);
}
}
is it possible that it might be that the table is a private static?
Answer by ValkaiIcai · Sep 02, 2016 at 01:37 AM
@Persious I don't know if this is your issue, but it could be that Array.Remove(string) isn't an existing function. Try:
AllQuestions.RemoveAt(i); //This function removes the i value from an array, i.e. RemoveAt(0) removes the first value in an array
Answer by Persious · Sep 03, 2016 at 11:54 AM
@valkailcai I changed from Remove to RemoveAt(i), but it's still doing something weird.
I uncheck all the categories (Which means the list should be empty), then I Debug.Log(AllQuestions.Count) before and after the loop and for some reason it debugs 42 before the loop which is alright, but after the loop is says 21, which makes no sense as it should show 0.
Your answer
Follow this Question
Related Questions
List<>.Add : Object reference not set to an object, but nothing is null 1 Answer
How to remove an item from a list of custom variables 1 Answer
Insert string into empty list at a specific index 0 Answers
Adding a unique element from one list to another list 2 Answers
How to get all children of a Gameobject with a certain component 2 Answers