- Home /
 
Array of abstract class in inspector
Hey people :)
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? :)
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{
         singleTarget,
         teamTarget,
         allTarget,
         randomTarget,
         lineTarget,
         rowTarget,
         singleTargetFront,
         lineTargetFront,
         adjacentTarget,
         crossTarget,
         noTarget
     }
     
     protected targetPattern myPattern;
     
     public abstract bool Action(BattleParticipant[] target);
     public abstract void UpdateStat(int neoStat);
     public abstract string GetName();
     public abstract targetPattern GetTargetPattern();
     
 }
 
              You might have to add the [Serializable] tag to each of the classes you want to use, and possibly the abstract one, too. It is possible, however, that this behaviour is not possible, though I sincerely hope it is, as it has many potential uses.
Indeed, I believe I have tried doing this, but I'll just try it out one more time :3
Hmm I tried adding the [System.Serializable] to both my abtract class, my inhertitances of that class, my Player and BattleParticipant which is were I want to make it visible, but it didn't work :/
Just a random thought, perhaps useless, is it possible Func would work better than Action here??
Thanks for the comment Fattie :3 However I believe Actions is better suited for this specific case ^^
Answer by whydoidoit · Sep 11, 2013 at 07:31 AM
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: http://answers.unity3d.com/questions/14877/can-a-custom-inspector-serialize-a-list-of-derived.html).
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>())
    //etcetera
 
              I was going to suggest interfaces... don't know why I left that out XD +1
Answer by Benproductions1 · Sep 10, 2013 at 11:38 PM
Hello,
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,
Benproductions1 
Thank you for your quick response :) I think I got what you mean, but for whatever reason, I took a step closer to achieving what I needed. I now have another problem :/
Here's an image of my inspector, where I finally got it to show my Skill array by inheriting from $$anonymous$$onobehaviour :) However if I try to add a Class which extends from Skill, such as _SkillFire into the Array, it won't let me :)
http://i.imgur.com/bS59UDx.png
Do you have an idea of why that might be? :3
Your answer