I need to be able to add and remove things from an array of custom structures.
In my JS file I have:
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?
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
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.
Initialises one of Unity's builtin/.NET arrays. These cannot be resized, but are fast and exposed in the Inspector.
Unity's reference asserts it is easy to convert between them to cover all requirements. Check out the full page here.
answered Dec 07 '10 at 11:27 PM
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.
answered Dec 07 '10 at 11:18 PM