- Home /
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
- Array documentation.
- @SerializeField documentation.
- @Serializable documentation.
- @NonSerialized documentation.
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).
Answer by Mike 3 · Dec 09, 2010 at 10:22 PM
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)
Your answer

Follow this Question
Related Questions
Does XML Serialization Support "Class Object" Arrays? 1 Answer
Cutom Array Inspector in a Custom Inspector 1 Answer
Saving array data? 2 Answers
C#-XMLSerialize a Struct with Vector3 Array in it 2 Answers
Serialize an array of custom objects 3 Answers