Issue with JSON serialization in Unity
<p>I'm trying to serialize an array of custom Monobehaviours, using JSON. I know it can't be done directly, so I'm using a wrapper class and serializing that instead.<br><br>This is the wrapper object</p>

<pre>[System.Serializable]  <br>public class WavesCollection {<br>    public Wave[] waves;<br>}</pre>

<p><br>That's the object being wrapped (only the important bits of it)</p>

<pre>[System.Serializable]<br>public class Wave : MonoBehaviour {<br>    [SerializeField]public float[] AppearTimes;//at that time should the n-th enemy appear;<br>    [SerializeField]public Vector2[] positions;//where should the n-th enemy appear;<br>    [SerializeField]public EnemyType[] EnemiesToAppear;//what enemies should appear<br>}</pre>

<p><br><br></p>

<pre>        wavesArray = new Wave[] {thisWave, thisWave, thisWave};<br>        WavesCollection collection = new WavesCollection();<br>        collection.waves = new Wave[10];<br>        wavesArray.CopyTo(collection.waves, 0);<br>        StreamWriter sw = File.CreateText(WritePath);<br>        string json = JsonUtility.ToJson(collection);<br>        sw.WriteLine(json);<br>        sw.Close();</pre>

<p><br>And that's the code that does the serializing. Basically I've got an array of non-null Wave objects and I copy that to the wrapper object. I then try to write that in a file and the output is</p>

<pre>{"waves":[{"instanceID":-99992},{"instanceID":-99992},{"instanceID":-99992},{"instanceID":0},{"instanceID":0},{"instanceID":0},{"instanceID":0},{"instanceID":0},{"instanceID":0},{"instanceID":0}]}</pre>

<p><br>Could anyone give me a few directions on what I'm doing wrong?</p>