I'm working in Unity, and I have arrays of different types which I need to check and manipulate. After some issues with the Reference Type I found the following solution:
I have created the class "ArrayHelper" like this
public class ArrayHelper {
public static void TrimArray<T>(ref T[] array)
{
T[] newArray = new T[array.Length - 1];
for (int i = 0; i < newArray.Length; i++)
{
newArray[i] = array[i];
}
array = newArray;
}
}
And I use this method like this:
void Start()
{
ArrayHelper.TrimArray(ref myArray);
}
While this works, I don't think it is really elegant. I've found this thread where they show how I could do this instead:
void Start()
{
nextPaths.Trim();
}
To do this I have changed the ArrayHelper class to this:
public static class ArrayExtensionMethods {
public static void Trim<T>(this T[] array)
{
T[] newArray = new T[array.Length - 1];
for (int i = 0; i < newArray.Length; i++)
{
newArray[i] = array[i];
}
array = newArray;
}
}
When I call the method it doesn't change the array. I suspect this is due to references. Is there a way where I can manipulate my array like this which works properly?
Note: There are also other methods in the 'ArrayExtensionMethods' which can manipulate the array in way more complex ways. Therefore I'm looking for a general solution to my problem instead of a solution which I can only use to replace the "Trim()" method.