- Home /
Unable to completely clear a list
Hello, So I have a simple list where I store a set of enemy spawn points. Every so often, the spawn points change. So whenever I create new spawn points I clear the list using List.Clear() and destroy the old spawn points, create new spawn points and add them to the list. Easy, right?
Well the problem is when I clear the list using List.Clear() (or setting it equal to a new List) it leaves behind missing elements. What it looks like:
This is my code:
ClearSpawnPoints function:
void ClearSpawnPoints()
{
Debug.Log("Clearing Points");
enemySpawnPoints.Clear();
Debug.Log("Cleared Points! Count is: " + enemySpawnPoints.Count );
}
Update Function:
if (enemySpawnPoints.Count > 0)
{
Debug.Log("Current Count: " + enemySpawnPoints.Count);
//Do Stuff
}
If it helps at all, when I clear the list, it is saying that the count is equal to 0. In the update function however it is saying that the list is equal to the previous list amount + the current List amount:
The red line is the point that I clear the list and create new ones. I only create 11 new points. I have no idea why this is occurring. If anyone has seen anything similar to this and why it might be doing this, please let me know.
Thank You in advance.
Would you $$anonymous$$d posting your full script here? (assu$$anonymous$$g it's not spread out over multiple classes). I'm interested to see how you're adding the new spawn points.
So a random set of rooms gets created periodically (when i press the return button for testing purposes). Each room is a prefab with a set amount of spawn points (empty game objects with the class enemyspawnpoints). These rooms are instantiated, and so are the spawn points. I then use GetComponenetsinchildren () to get all of the spawn points.
Which after a quick debug as I am writing this, it is picking up all of the old spawn points. So... narrowing it down. Does seem like it has something to do with the either deleting or creating the old spawn points.
Answer by joelcomberiati · Apr 12, 2020 at 04:15 AM
Figured it out, thanks to @Addyarb for pointing me in the right direction. When I was removing the previous spawn points using destroy. The actual destruction of these spawn points was occurring after getting the new spawn points. At first I tried destroy immediate. Which worked, but obviously in an attempt to avoid this I simply set them to to inactive, then destroyed them. Worked like a charm!
Answer by whogas · Apr 12, 2020 at 04:18 AM
I think we might need a bit more info about your code. Is this list clear and update check both in the same class? How are you destroying the enemy spawn points and then adding them back to the list?
Your answer
Follow this Question
Related Questions
How to modify array values? 1 Answer
A node in a childnode? 1 Answer
List is created correctly in Start() but returns size 0 in play mode 3 Answers
Why the List in the class when using it is not the same in another class ? 1 Answer
How to compare a string in a list with a gameobject.name from another list of gameobjects 1 Answer