Okay, this is my attempt at adding a remove function for the built-in unity arrays. I’ve played around with the variables and for loop limits for hours and nothing seemed to work. What it does is it takes an array, then puts it’s contents into another array, then it takes each element in the second array after the element that has to be removed it will just replace it with the next element, then it will go and put those elements back into the modArray but without the last element, so theres one less element which basically removes that element. But for some reason this isn’t working, as it removes all but the first element in the array, it’s based off of my add function which works flawlessly and faster than the standard JS arrays.
function RemoveObject () {
var refArray : GameObject[];
var index : int;
var index2 : int;
refArray = modArray;
var length = (refArray.length -2);
modArray = new GameObject[(length)];
var modLength = modArray.length;
var refLength = refArray.length;
for(var i = 0; i < modLength; i++){
index2 = i;
if(refArray*.transform.position == modElement.transform.position){*
-
index = i;*
-
}*
-
}*
-
if(index2 < modLength && index > 0){*
-
refArray[index2] = refArray[index2 +1];*
-
}*
-
for(var x = 0; x < modLength; x++){*
-
modArray[x] = refArray[x];*
-
}*
}
There are several things strange:
- Your new array is 2 elements shorter than the original one. when you want to remove one element you should decrease the lenth by 1, not by 2
- Your index2 variable will always end up with the highest value possible in your new array. That’s pointless since you could always use modLength-1
- It seems you’re searching for a certain position. If you want to search for a certain object it’s better to compare the objects itself and not their positions. If you just want to test for the position it’s fine that way, but keep in mind that the position have to match “exactly”. The position (1.0, 50.5, 45.87) and (1.0, 50.5, 45.8700001) appear at the same spot, but they’re not equal.
- When you search for an index and you’ve found it, you should break the loop since it’s pointless to iterate through the whole array.
- You copy the second last element of your original array at the last position of your new array, but then you run the for loop at the end which just overwrites all elements and just copies all elements from the original array into the new one but removes the last two objects…
- Last thing is your function seems to work with certain class variables, so it’s not a general approach.
A function to remove a certain gameobject from a gameobject array could look like this:
static function RemoveGO(arr : GameObject[], obj : GameObject)
{
var index = -1;
for (var i = 0; i < arr.length; i++)
{
if (arr *== obj)*
{
index = i;
break;
}
}
if (index == -1) // the element wasn’t found
return arr;
var tmpArr = new GameObject[arr.length - 1];
for(i = 0; i < index;i++)
tmpArr = arr*;*
for(i = index+1; i < arr.length;i++)
tmpArr[i-1] = arr*;*
return tmpArr;
}
To use this function you would do this:
modArray = RemoveGO(modArray, modElement);
Anyway, it would be much easier to use a List. A List uses an native array internally and provides all those functions already. To use a generic list you have to import the System.Collections.Generic namespace:
import System.Collections.Generic;
var modArray : List.;
modArray = new List.();
// Add an element at the end:
modArray.Add(newGO);
// To remove an element
modArray.Remove(modElement);