How can I serialize a private Array in JS?

Problem

I have a private Array in my java script code. How can I make sure Unity3D serialize it?

private var myArray : Array = [];


This question has a clean, accepted answer. Please also see the related material for more details.

See also

Remarks

  • Unity doesn't serialize a List of Lists, nor an Array of Arrays.
  • Unity doesn't serialize Dictionary.
  • Unity doesn't serialize static fields.
  • Unity doesn't serialize properties.
  • If an object occur more than once in a collection, that object is serialized for each occurance of it (creating clones).

First, you'll need to use an array/collection with a type which can be serialized, and then add @SerializeField above it, i.e.

@SerializeField
private var myArray : Transform[];

or

@SerializeField
private var myList : List.<Transform>;

You can't use a basic javascript array because it's untyped - use either the builtin or List type for it. The latter will work very similar to Array (it has .Add etc)