3
\$\begingroup\$

I have a class hierarchy with an abstract parent class.

I would like to have a field of this type exposed to the inspector but it appears to not display.

This test code:

[Serializable]
public abstract class AbstractClass
{
    public string testField;

    public AbstractClass(string testValue)
    {
        testField = testValue;
    }
}

[Serializable]
public class ConcreteClass : AbstractClass
{
    public ConcreteClass(string testValue) : base(testValue)
    {
    }
}

public class MonoBehaviourClass : MonoBehaviour
{
    public AbstractClass abstractClass = new ConcreteClass("abstract");
    public ConcreteClass concreteClass = new ConcreteClass("concrete");
}

Produces this result:

enter image description here
As you can see the field for the abstract class is missing.

How can I expose a field of an abstract type to the inspector?

\$\endgroup\$
2
\$\begingroup\$

The answer is that you can not, Unity uses reflection to find fields to show and the fields shown are the public fields of serializable classes and private fileds with SerializedField attribute (inherited fileds included) but not from abstract classes.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.