- Home /
Assigning child script to parent field in Inspector
In my project, I have gone about creating character Abilities (skills, essentially) by creating a base Class, Ability, that will be inherited by all Abilities created. It's an abstract class with some undefined methods that will change based on the Ability that the child will be, as well as some properties whose values will change based on the Ability as well.
Overall it is working, but now I've gotten to the point where I need to be able to dynamically add and remove them mid-game from the Player object (and eventually NPCs, monsters, etc.), however I'm running into some issues.
On my Player script, I've given it a size 10 Array of Abilities called skills - By my game's ruleset, the Player can only have 10 Abilities active at any time, so the skills array is specifically the skills that are actively set for the Player's use at that moment. The problem is that for testing, and eventually non-dynamic setting of these (IE, a monster may only have 1 Ability ever, regardless of the point in the game, so I'd mind as well set that into its prefab), I've attempted to add a script that defines a for-testing Ability named BasicAttack. I cannot add this script directly to the array in the Inspector. I imagine it's because it's a Script, and technically not an Ability flat out.
Here's my question: Do I need to make a prefab for every Ability, and simply give it the script of that Ability, then give that AbilityPrefab to the Inspetor, or what? That doesn't exactly seem correct to me because a prefab is a GameObject, not an Ability (or whatever I want it to be). How exactly should I be handling this?
I've also seen a couple of questions that mention that you cannot place a child class directly into the Inspector - It doesn't care about Inheritance or Polymorphism at all. If this is the case, how do I go about handling this issue? I was reading something about making a Custom Inspector object that represents the base class, but all the information I can find about this is vague and the documentation has no C# reference, only UnityScript, which doesn't really help me because the syntax is significantly different from what I can tell.
Any information in regards to this problem would be greatly appreciated, thanks in advance.
Answer by Polantaris · Jan 02, 2014 at 03:14 AM
I found the answer:
http://wiki.unity3d.com/index.php?title=CreateScriptableObjectAsset
This page showed me how to create a ScriptableObject Asset, which I can then place into my Array directly through the Inspector when necessary. It doesn't seem like the older polymorphism questions I saw when searching for a solution apply anymore, I was able to place my BasicAttack testing Ability into an Ability field and had no problems.
Your answer
Follow this Question
Related Questions
How to set a default prefab on a C# script? 1 Answer
prefab contents of the Custom inspector disappear. 0 Answers
Prefab not inheriting GameObject when instantiated 1 Answer
Linking prefabs 0 Answers