Is there any way to use a System.Object class in a class which extends ScriptableObject for use in AssetDatabase.CreateAsset? Try as I might, unity does not want to serialize the System.Object and anything with an System.Object type gets thrown away on serialization.
All classes inherit from System.Object
(usually indirectly).
You can certainly create a class which inherits directly from System.Object
or UnityEngine.ScriptableObject
, but not both at once (you can only directly inherit from one class). I’m not sure if that’s the question you’re asking, though.
Ignoring questions of namespace and scope, an object can certainly contain references to objects of any other type. That says nothing about whether or not those references will be serialized, of course, which seems to be what you’re after.
I’ve had some luck getting Unity to serialize custom classes like so:
- Create some class which inherits from
MonoBehaviour
- Create some data class which doesn’t explicitly inherit from anything, and give the class the
System.Serializable
attribute - Your behaviour script should contain a list of data nodes, exposed to the inspector (
public
,SerializeField
, etc) - Your behaviour script should only use
new
to create the list if it doesn’t already exist (from serialization)
I assume it’s possible to do more than that, but I haven’t had many chances to explore this in depth.
One important caveat with Unity serialization: it is not polymorphic. If the list you’re serializing has mixed types, Unity will serialize every node as the list’s generic type.
In my experience, this is one of the trickier topics with Unity dev. You’re sort of “going off the map”.