- Home /
Question by
ramonleenders · Dec 13, 2011 at 04:07 PM ·
gameobjectarraydelete
Remove via inspector added objects within array
What I have is a script attached to an empty object. This script has multiple variables and arrays. One of those arrays is an array with game objects. What I would like to do is, at the start, remove some of the objects (I can set a variable to 12 for example, and the object array is 16, so I want to remove the last 4). I tried both Destroy and DestroyImmediate, but these don't seem to be the right approach. This is what I have, and what's not working. Also, I want the current order of the array to remain, but afterwards I want to shuffle. It seems that somehow it shuffles my complete array, because of the wrong deleting process?
if(numberOfTiles > tileObjectsInGame.length){
numberOfTiles = tileObjectsInGame.length;
}
else {
for (i = numberOfTiles; i < tileObjectsInGame.length; i++) {
Destroy(tileObjectsInGame[i].gameObject);
}
}
RandomizeBuiltinArray(tileObjectsInGame);
static function RandomizeBuiltinArray(arr : Object[]){
for (var i = arr.Length - 1; i > 0; i--) {
var r = Random.Range(0,i);
var tmp = arr[i];
arr[i] = arr[r];
arr[r] = tmp;
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613062037im_/https://answers.unity.com/themes/thub/images/avi.jpg)