4
\$\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\$
0

2 Answers 2

3
\$\begingroup\$

It's possible natively since 2019.3 release via [SerializeReference] attribute.
Unity's SerializeReference documentation.

\$\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\$
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .