Array of abstract class in inspector

Hey people :slight_smile:

I’m kinda new to the whole making custom inspector scripts, but I have this problem that I was wondering if is even solvable.

So what I want to do is have multiple classes extending from my Skill class which are all unique, this is not a problem, however I’m looking to be able to attach these to a character through the inspector into an array of Skill.

Is that possible and is there a smarter way of doing it? :slight_smile:

I have a class which contains the line of code that looks like this:

public Skill mySkills = new Skill[8];

And the abstract class Skill which looks like this:
public abstract class Skill{

	public bool targetMyTeam;
	public bool targetMustBeEmpty;
	protected string name;
	public enum targetPattern{
	protected targetPattern myPattern;
	public abstract bool Action(BattleParticipant[] target);
	public abstract void UpdateStat(int neoStat);
	public abstract string GetName();
	public abstract targetPattern GetTargetPattern();

I’d be tempted to define those attributes in an interface and have your components implement that interface - Unity has problems with serializing polymorphic objects see this: Can a custom Inspector serialize a List<> of derived classes? - Unity Answers).

If you put them in an interface you can just attach any “skills” you like to any game object and find them all easily using something like this:

  using System.Linq;


   foreach(var skill in GetComponentsInChildren<Transform>(true).Where(t=>t is ISomeInterface)).Cast<ISomeInterface>())


For any class to be attachable to a GameObject, it needs to inherit from MonoBehaviour or even just from Component. There is no way around this.

Also note that if you want to add a script to a GameObject that does not match the name of the file that contains it, you will need to do it manually using AddComponent. I haven’t tested anything, but you should be able to make a list (don’t make an array) of an abstract class.

Hope this helps,