Script as variable

Hi.
I was trying to make some usable abilities for characters in my game. To make it easier I’ve made Scriptable Object that contains hero statistics, but i also wanted to add abilities there.

Each abilities set(different for each hero) is stored in its own script. For example:

  • HeroX_abilities.cs
  • HeroY_abilities.cs
  • etc.

The problem is that i can’t make a variable in Scriptable Object which could contain script,


I can do:

public HeroX_abilities ability;

But this way i would have to make separate statistics script/scriptable object for each character in the game


Also can do this:

public MonoBehaviour abilities;

but it seems that it can only store script from existing object, not from project files.


What I’m trying to do:

public 'ScriptVariable' abilities;

So I don’t have to make separate abilities script and I can drag and drop scripts from project files to this variable.

Is there any way to do this? or maybe I’m doing this in wrong way and there is simpler solution?

HeroX_abilities.cs, HeroY_abilities.cs and so on should be scriptable objects as well. You would create instances of each ability, which would make it a lot easier to tune them, and create variations of the ability.

So instead of having FireBall.cs and IceLance.cs, you have one RangedDamage.cs, which you create several instances of. Each of those scriptable object instances would have attributes that you tune, particle effect prefabs, and other scriptable objects linked for things like status effects and whatever.

Another solution would be to make some kind of manual link. Your heroes could have a field with the type MonoScript, which is the (UnityEditor only, sorry about that) runtime representation of scripts.

As you drag-and-drop the script in to the ability-field, you would have the inspector store the type of the ability, and then instantiate that on awake.

Sadly, Unity cannot serialze the type Type, so you’ll have to store a string-representation of the ability type, and use reflection to load it during that awake.

This solution is most in line with what you’re looking for, but I prefer the flexibility and power of the first solution, so I included that.