x


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).
more ▼

asked Dec 09 '10 at 09:28 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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)

more ▼

answered Dec 09 '10 at 10:22 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1363
x181
x108

asked: Dec 09 '10 at 09:28 PM

Seen: 2352 times

Last Updated: Dec 09 '10 at 11:22 PM