I recently came across an issue where I had multiple scriptable objects defined in the same file. After some searching, I came across this issue on the issue tracker, which lists the problem as “won’t fix.”
As multiple instantiatable scriptable objects defined in one file is not supported, I was wondering if the situation below would also not be supported.
// File 1 defines 2 abstract scriptable object classes
public abstract class AbstractClassA : ScriptableObject { }
public abstract class AbstractClassB : ScriptableObject { }
// File 2 defines an instantiatable scriptable object class.
public class InstantiatableClass : AbstractClassA { }
I have tried this locally, and everything seems to be working (i.e. I’m not losing references between editor startup as far as I can tell), but I’m not sure if this might just be due to some favorable result in a race condition, or there would be other reasons why this isn’t supported.
So the thing with Unity is that assets (prefabs, so assets, gameobjects in a scene, etc), are associated with the script based on a ‘guid’. This ‘guid’ is stored in the meta file that goes with the code file. So when the script gets serialized into said asset file, it gets looked up via this guid. There’s also a need for the filename to match the class name as well in this.
You could have the situation you have above as long as the cs file is named “InstantiableClass.cs” and it’s the only class that you expect to be created as an asset.
This works since the guid is associated with the class that has the name of the file. Any other classes in the file are ignored in regards to the serialization.
…
This also means that this guid thing really only pertains to serialization. You could create instances of any class at runtime. You just can’t save it to disk as an asset (may it be an *.asset, *.prefab, or in a *.unity scene).