Hi, trying to tackle this little issue, hopefully the Unity inspector supports it.
Given these two sample classes:
public abstract class BaseClassA : MonoBehavior {
}
public class ClassA : BaseClassA {
}
public class ClassB : MonoBehavior {
public List<BaseClassA> Collection;
}
I am able to designate the list size in the inspector, but I can not attach (click and drag) the inherited ClassA to any of the elements in the list.
Any thoughts on this?
Edited by AlwaysSunny : to improve specificity.
Just tested it again in the newest version of Unity (but i was quite sure that it will still work) and it worked just fine. Maybe you tried to drag a script file directly to your “Collection” list which of course isn’t possible.
When you drag a script file you actually drag a MonoScript which represents a class and not an instance of that class. You can “drop” such a class onto a GameObject to create an instance of it (which will be attached to that GameObject).
Your “Collection” list actually holds references to instances of classes which are derived from BaseClassA. So you can only drag and drop instances to that list. To do that you have several options:
- If your instance of ClassA is attached to another GameObject in the scene, just grab the GameObject in the hierarchy where a ClassA script is attached to and drag it into a slot of your “Collection” list.
- If your instance of ClassA is attached to the same GameObject as your ClassB, you can grab the header bar of the script component in the inspector and drag it onto your list slot.
- If you have multiple instances of a compatible class (anything derived from BaseClassA) on the same GameObject, you have to use another inspector window and lock one to your object which holds ClassB so it stays visible. Now simply select the GameObject which has the ClassA instances attached and again grab the header bar in the inspector of the instance you want to put on the list and drag it over into the list of ClassB in the other Inspector window. (To open another inspector, just click any window menu at the top right and select “Add Tab” → “Inspector”. You might want to arrange the two inspectors next to each other so you’ll see both). To lock an inspector to the current selection, just press the “pad lock” icon next to the window menu button.