Resizing arrays, and what is the default array type anyway?

I need to be able to add and remove things from an array of custom structures.

In my JS file I have:

var stops : TourStop[];

where TourStop is a class containing various things. This 'stops' variable shows up in the Inspector, and I can edit it there (to initialize it).

I need to resize the array (add/remove) at runtime.

Is this a 'built-in' array? Or a JS array? Or something else?

I tried

stops.length = stops.length + 1;

But despite what the documentation says (assuming this were an Array), 'length' is read-only. So I'm not quite sure what this thing is so I don't know which API's to call to muck with it.

A built-in array `instance : T[]` is a fast, fixed-sized array. To resize such an array, you would need to create a second array of the new size and duplicate the items from the old array over. It may show up in the inspector as its content's type is known, but only if it is a serializable type.

A "javascript" Array `instance : Array` is a not-so-fast, dynamically-sized array. It does not show up in the inspector as its content's type is unspecified.

See here for more about arrays. Note that you can convert between built-in arrays and Arrays, so if you start with an array and then convert it to an Array, change the Array and then convert back, you can shorthand the process, but this is hardly the most efficient route.

If you wanted a dynamically sized array-like structure that serializes in the inspector, you want a System.Collections.Generic List of a serializable type. See here for more about serializable types.

Using:

var stops : TourStop[];

Initialises one of Unity's builtin/.NET arrays. These cannot be resized, but are fast and exposed in the Inspector.

var stops = new Array();

Creates a more traditional (to programmers) dynamic Javascript array. You can add new values onto this using stops.Push(value), and alter its size. The downside is that they aren't available in the Inspector window.

Unity's reference asserts it is easy to convert between them to cover all requirements. Check out the full page here.

You probably need to create a new array with length = oldArray.length+1 and copy over the objects inside.

What you probably want to do, is to use a List.