I have a scriptable object that takes in an abstract class for some polymorphic behaviour. The problem I am having, is I don’t know what the abstract class has to extend from in order for me to be able to assign script implementations of the abstract class to the scriptable object. I tried inheriting from UnityEngine.Object, however this didn’t work.
I basically want to reference scripts the same way the animation events system does but on a scriptable object.
If I understand you correctly, I don’t think you can get what you want.
ScriptableObjects are assets that sit on your hard drive.
Instances of scripts are in a scene at runtime, so a scriptable object asset could not reference it “in advance.”
You can however drag a reference to a prefab into a public inspector field in your ScriptableObject, but keep in mind that is a reference to the non-running asset on your disk, and when you Instantiate a new copy of that prefab into a scene, it will obviously not go fixup everywhere that was pointing to it. But it might be good enough for you to direct your initialization code to use this approach to connect bits of behavior together using primitive behavior prefabs.
If you’re looking for some kind of data-controlled factory pattern, like “for this ScriptableObject, I want you to use this MonoBehavior on a GameOBject,” I think you are stuck with using the now-deprecated .AddComponent(string className). Because it is deprecated I don’t recommend it, as it will go away sometime.
I thought that because the animation events seems to allow you to reference assets, then I could do the same some how with scriptable objects… However of course Animation event system expects you to have that object on the gameobject playing the animation I believe… Not sure what happens when you apply things that aren’t scripts.
I guess you’re right. I have no way of getting an instance. Using a scriptableobject or a prefab is just an ugly method to me :<
I think the animation event stuff does everything via strings and late lookup, since pretty much 100% of internal animation connections are also via string.
I have used a lot of different CPUs and GPUs in my lifetime and I have yet to come across one that passes judgement on ugly methods.