How do I Resize an Array in JS?

So, Newbie me just got introduced to .NET array methods by our shepherd Eric5h5. Tried System.Array.Copy, nearly made me cry with a mixture of joy at the prospect of not having to write a new function for each new class I’m resizing an array of, and frustration with having already typed so much useless code… I then tried System.Array.Resize(myArray, myArray.length+1); nope. Is my syntax wrong, or is there only a limited set of .NET methods which we can use in Unity’s javascript? If so, where’s the list? And are there any iOS compatibility issues? If yes, it would at least make my array manipulation skills less obsolete…

Thanks in advance!

System.Array.Resize is a template method and the only way I can get it to work with JS is to explicitly define the template parameter. So yes, your syntax was a bit off there, but here’s how it should look:

var myArray : int[];
System.Array.Resize.<int>(myArray, myArray.length + 1);
print(myArray.length);

Given myArray is initially 5, running this script will print “6”. I hope this little snippet is self explainable but if you have questions, just post a comment to this answer and I or someone else will get back to you.

The code you posted would generate this error message:

Assets/MyArray.js(2,20): BCE0023:

No appropriate version of ‘System.Array.Resize’ for the argument list ‘(int, int)’ was found.

The method clearly is there, it’s just hidden as a template method (meaning you can replace the type of the array). The syntax for specifying the template parameter is added as .<T> to the method name, where T is the type of elements in the array.