I have a buff system I’m trying to prototype. I have a buff object being created and added to an array. I’m printing a list to the screen to see how many items are in the array. Then I’m iterating through the array and calling the Destroy method on all my buff objects. Weirdly not every object is being iterated. Only every other item and I don’t know why.
Created 5 buff objects that get added to an array.
I think the issue is what array.Remove is doing. Seems to be resizing my list before I finish iterating through it. So how do I iterate through a list removing specific elements?
I learned that removing the item at element i calls for the array to arrange itself so the item at element 0 that has been removed now contains the item at element 1, and by the time my iteration goes to 1, element 1 now contains what was in element 2, etc…
Okay, so since its doing that, I’ll just always get the element at 0 since all the elements will shuffle down after one is removed. My loop still only iterated 3 times. I was very confused. Then I tried the same logic in reverse always getting the last element in the array. Still only 3. So what the fat?
Seems loop nodes (For and ForEach) are constantly pooling new data for parameters during iteration instead of caching it in local scope like for(int i = 0; i < array.length; i++) would. So once I cached what array length is before my loop started, it began iterating as expected.