There is an array GameObject consisting of 4 elements.
How to shift the elements of an array if we assume that element 2 is missing? 3 and 4 should move up one position.
for (int i = 0; i < obj.Length; i++) {
if (obj *== null) {*
// Shift next elemen up;
}
}
@Smart2x2 When I’m pruning arrays like this, I tend to create a list from the array, loop backward through it, and remove any indices with null, then ToArray the list to the original array. An example method to prune nulls:
<pre> private GameObject[] _removeNulls(IReadOnlyList<GameObject> sourceList) { List<GameObject> tempList = new List<GameObject>(sourceList); for(int index = tempList.Count - 1; index > -1; --index) { if(tempList[index] == null) { tempList.RemoveAt(index); } } return tempList.ToArray(); } </pre>