Thatās what I thought. This doesnāt work this way since a script asset is actually a MonoScript instance which is actually a TextAsset. However the MonoScript class is an editor only class as this is how script assets are represented in the editor.
I made a SerializableMonoScript class which you can use to essentially serialize the actual type information inside the inspector by dragging a script asset onto the field. It actually stores the assembly qualified type name of the type that the MonoScript implements. At runtime you can read the āTypeā property which gives you the System.Type which you can use in AddComponent, or when you use the generic version, it has a neat helper method āAddToGameObjectā which allows to add this type as a component to the given gameobject and return the casted instance.
The generic version also allows you to restrict the types to a certain base class or interface. Note since I use an ordinary ObjectField you can actually drop any MonoScript onto that field, however incompatible types will be rejected with a log message. This is not the best user experience, however this way we get all the ObjectField stuff for free (selecting / pinging the script in the project, object picker).
So with this file in your project you can simply do
using B83;
// [ ... ]
public SerializableMonoScript<Skills> skillType;
and when you want to actually add this skill type as a component to a gameobject, you can simply do
Skills skillInst = skillType.AddToGameObject(gameObject);
or the manual way like this:
Skills skillInst = (Skills)gameObject.AddComponent(skillType.Type)
which does the same thing.
Note that this class also works for ScriptableObjects equally. The generic version also has a dedicated āCreateSOInstance()ā method.
Just a short warning: Since a āSerializableMonoScriptā actually just stores the assembly qualified type name, āreferencesā stored that way would break when you refactor the type name or move it to a different assembly. So keep that in mind.
ps: I also made a SerializableInterface some time ago which allows you to store and filter a UnityEngine.Object instance reference based on an interface or base class type. So this also works for MonoBehaviours and ScriptableObjects. Since itās generic you also get easy access to the properly casted instance. When you drag a gameobject onto that field and multiple components match the filter, a drop down pops up that lets you select the instance you want.