Serializing a collection of ScriptableObjects to an ASSET file?

Hi all,

I have tried to make my code as simple/reduced as possible to try to understand what’s going on.
I have a class representing the data I want to serialize, like this:

using UnityEngine;

public class TestData : ScriptableObject
{
	public int theValue;
}

… and a class which represents a serializable collection of TestData objects, like this:

using UnityEngine;

public class TestCollection : ScriptableObject
{
	public TestData [] myArray;
}

Ok, so what I want to do is to create a collection of data objects and save this collection to an ASSET file, which will be loaded in runtime through Unity’s Resources.Load() method.
I have created a simple script which creates two menus in Unity Editor: one of them is used to create an asset file containing a predefined TestCollection of TestData objects, and the other one is used to load the saved TestCollection from that created file and print the values of all the TestData objects contained in it.

using UnityEngine;
using UnityEditor;

public class TestMenu
{
	[MenuItem("TEST/Create asset")]
	public static void testCreate()
	{
		TestCollection newAsset = ScriptableObject.CreateInstance<TestCollection>();
		
		newAsset.myArray = new TestData[3];
		for ( int t = 0; t < 3; t++ )
		{
			newAsset.myArray[t] = ScriptableObject.CreateInstance<TestData>();
			newAsset.myArray[t].theValue = (t+1)*100;   // initialize data objects with values 100, 200 and 300...
		}
		
		AssetDatabase.CreateAsset( newAsset, "Assets/Resources/MyTestAsset.asset" );
	}
	
	
	[MenuItem("TEST/Print values")]
	public static void testPrint()
	{
		TestCollection theAsset = Resources.Load<TestCollection>( "MyTestAsset" );
		foreach ( TestData x in theAsset.myArray )
			Debug.Log( x.theValue.ToString() );   // <-- EXCEPTION IS THROWN HERE
	}
}

Now, the problem is that Unity seems to be serializing the TestCollection object correctly, while it is not serializing the TestData objects in it.
If I click the “Create asset” menu and then click the “Print values” menu, the values of the objects stored in the asset file are printed correctly: 100, 200 and 300, respectivelly.
But then, if I enter in play mode and click the “Print values” menu, a NullReferenceException is thrown when trying to execute the “Debug.Log( x.theValue.ToString() )” line (“x” is null), which means that the serialization of the TestData objects have failed!

So, is there any way to serialize a collection of ScriptableObject objects to an asset file? What am I missing here…?

ScriptableObjects need to be serialized on their own. So you either have to serialize them as seperate assets or (what will be more common) add them to your base asset with AssetDatabase.AddObjectToAsset.