SerializeField in abstract class not appearing in inspector

48 views Asked by At

I'm running into what I think is probably a syntax issue. I have an abstract class:

public abstract class MyAbstractClass : MonoBehavior {
    public abstract GameObject ShouldBeSerialized { get; set; }
}

that has a property that I want to appear in the inspector for concrete class instances:

public class MyConcreteClass : MyAbstractClass {
    [SerializeField] public override GameObject ShouldBeSerialized { get; set; }
}

I've tried adding [SerializeField] to both the concrate and abstract definitions. I've tried adding [Serializable] to the abstract class definition. I've tried making the property both virtual and non-abstract in the abstract class definition. Perhaps there's a combination of the above that I've missed?

2

There are 2 answers

3
Evgenii Glebov On BEST ANSWER

Use [field: SerializeField] instead of [SerializeField]

[field: SerializeField] public override GameObject ShouldBeSerialized { get; set; }
1
WongWray On

I figured this out via trial and error. I changed the abstract class's definition to

[SerializeField] public GameObject ShouldBeSerialized;

And then don't override or reference the field in the concrete class at all. It seems to be working but I'm getting a

SerializeField attribute is invalid or redundant

warning.