Asset Bundles / Prefabs and Serialized Scriptable Objects

My Scriptable Objects do not appear to exist in my Asset Bundles. I’m tracking down the information I think I need to make this work, but I haven’t found a clear set of instructions yet.

I have a Monobehavior Component that has a list of ScriptableObjects that, in turn, have lists of SerializableObjects (some of which do that again…). Everything serializes/unserializes just fine in the Editor, but doesn’t make it into the Asset Bundle. The component’s lists are the correct size, but filled with null references. Note that, in many cases, the inheritance is Foo : Bar : ScriptableObject, and there are references both to other scriptable objects, and other game objects.

So - how can I get the serializations properly stored in and then loaded with the Asset Bundle?


To be clear, the observed problem is that when I load the asset bundle into the app, when trying to ForEach(o => o.Thing()) on the list of scriptable objects, I get null reference errors. List is of the correct size, but filled with null. We then found a script to import an asset bundle back into the Unity Editor, and when inspecting those objects, null reference error are thrown on a similar line of code. My interpretation is that the serialization is failing, as this looks the the same as the problems I was having prior to resolving the serialization issues.

Edit 2: The consensus is that you want to turn the scriptable objects into assets, and then reference those. I am now confused and trying to answer the how (CreateAsset or AddToAsset and what path) and the where - where do I put this code so it’s activated and used when the object is turned into a Prefab / changes are applied?

Edit 3: My problem is actually resolved by using monobehaviors (I’d gotten it into my head that you can’t have more than one component of the same name on an object, but you totally can) - however, I’m going to continue poking at this until an answer is found for those of you who can’t just switch to a MB. To be explicit, inherit from MonoBehavior rather than ScriptableObject, and ScriptableObject.CreateInstance becomes gameObject.AddComponent (where gameObject is an actual object).

Are you sure you have stored the ScriptableObjects as an asset in your project? And have you add it to the assetbundle as well?

ScriptableObjects work way different than classes that have a System.Serializable attribute. ScriptableObjects are not serialized along with other objects. Only the reference is serialized. When they are present in a scene they might get saved along with the scene, but not for standalong assets like prefabs or assetbundles.

System.Serializable classes are actually part of the containing class. So when the MonoBehaviour gets serialized the custom class will be serialized along with the MonoBehaviour instance. That’s why no referencing is possible with those classes. If two MonoBehaviours hold a reference to the same System.Serializable class, each MonoBehaviour will have it’s own “instance” of the class after deserialization.

ScriptableObjects, when stored as assets, can be referenced like any other asset (Texture, Mesh, prefab, Material, …).

You can use the AssetDatabase class to store an ScriptableObject instance as asset. The Unity editor is the best example how you could use a ScriptableObject. Just look into the “ProjectSettings” folder of your project. It contains serialized ScriptableObjects saved as assets.