If I have an array with some empty slots, is there any function I can call which will remove them? I want to use something like Array.Clear () just Clear () removes everything and I just want to remove the empty slots. How can this be done?
You will need to iterate through your array and remove the holes manually. There is no built in "Remove Empty Slots" functionality.
Okay. This is what I have so far. Code (csharp): for (var i = 0; i < list.length; i++) { if (i == null) { // This doesn't actually delete the slot. list.RemoveAt (i)? } } Now my question is, How do I delete one slot of an array?
How are you using the array so that you see the empty slots? The Array type shouldn't actually ever have null entries because it is a list, not a real array.
The list is an array. I have 10 gameObjects in an array, so the length of the array is 10. After I destroyed 4 of those gameObjects I checked the array's length and it was still ten, so it must have some null slots. How can I get rid of only the empty slots so the length is 6? Thanks
You would have to use... Code (csharp): for (var i = 0; i < list.length; i++) { if (list[i] == null) { // This doesn't actually delete the slot. list.RemoveAt (i)? } }
Why don't you just remove it from the list when you destroy the object using SendMessage? Then you won't have to go through this cleanup loop.
If you did want some sort of clean loop you'll need to adjust the iterator and the amount of iterations to be performed. Removing an item from the array should change the length of the array (in JS). Code (csharp): var n:int = list.length; for (var i:int = 0; i < n; i++) { if (list[i] == null) { list.RemoveAt(i); i--; n--; } }