The question is answered, right answer was accepted
C# ArrayList Accessing and RemoveAt?
So I have an ArrayList defined and I'm feeding it information, once it hits 5 units large I want it to delete everything past the first position in the array I.e if the array contains [0,3,2,1,4] I want to kill everything that isn't 0.
To do this I am using the .RemoveAt(i);
function and it works to an extent as I can remove say 0,1,2 but when I try to use the third and fourth positions as i I get the error "Index is less than 0 or more than or equal to the list count" I assume this has something to do with the fact the position of the other elements changes as I delete one but I can't work out the solution. The aim is to remove all units except the first one so the Array List can exist.
if (spellInput.Count == 5)
{
Debug.Log("STORE!");
spellInput.RemoveAt(0);
spellInput.RemoveAt(1);
spellInput.RemoveAt(2);
spellInput.RemoveAt(3);
Debug.Log(spellInput.Count);
}
Any ideas?
Ok for anyone interested I worked it out on my own you can use .ReomveRange(index, count);
index is the point which it begins removing from and count is the number of elements it will remove, it seems to work perfectly!