Hi. I’m working on a waypoint system. I want to add and remove waypoints dynamically in the editor using an array. Creating new points is quite easy using:
if(list _== null) list *= new GameObject().transform;*_
When increasing the size of my array, the new transforms will pop up as intended. But I also want them to disapear when I decrease the size of the array.
Question: How can I dynamically destroy gameObjects when they are thrown out of an array due to me manually resizing it?
Problem solved! Now I use a second array which keeps track of my transforms after resizing. Here is the essence of my code:
//Create new handles and make sure they are individuals
for(int i = 0; i < list.Length; i++){
if(list _== null ) list *= new GameObject().transform;*_
* for (int j = 0; j <i ; j++){*
if(list[j] == list_) list = new GameObject().transform;
* }
}
//Destroy any unused handle*
* if(listCopy == null) listCopy = new Transform[0];
//Debug.Log(“List.length: " + list.Length + " listCopy.Length: " + listCopy.Length);
if(list.Length != listCopy.Length){
//Debug.Log(” != ");
if(list.Length < listCopy.Length){
//Debug.Log(“Destroying”);
int dif = listCopy.Length - list.Length;
for (int i = listCopy.Length - 1; i > listCopy.Length - 1 - dif; i–){
DestroyImmediate(listCopy.gameObject);
}
}
listCopy = list;
}*_