I have an array of game objects declared as:
public GameObject[] lifeMarkers;
I want to truncate the array by chopping off the last element.
I’ve tried Pop() and array.length = array.Length - 1 but I get this error each time:
Assets/GameController.cs(86,37): error CS1061: Type `UnityEngine.GameObject[]' does not contain a definition for `length' and no extension method `length' of type `UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)
I got both those commands from the unity script reference. Any advice?
Changing the size of arrays at runetime is not very efficient, as it involves a lot of memory copying. Consider other alternatives such as using a List, a LinkedList, or a queue. All of those (I think) contain a method of removing the last element.
If you need to change the size of an array at runtime, then try something akin to
`
int newLength = lifeMarkers.Length-1;
newLifeMarkers = new GameObject[newLength]
System.Array.Copy(lifeMarkers,newLifeMarkers,newLength);
lifeMarkers = newLifeMarkers;
`
Or alternatively, allocate enough memory (indexes) into your array and keep an int to keep track of the current last element.