Using Header with serialized properties

I saw this in a closed post

[Header("Movement Speeds")]
[field: SerializeField] public float walkingSpeed  {get; private set;}

The problem was that Unity did not like this. The Header attribute is only valid for fields, and properties (like walkingSpeed above) are not fields. So, we take a tip from the SerializeField and specify the target

[field: Header("Movement Speeds")]
[field: SerializeField] public float walkingSpeed  {get; private set;}

Job’s done.

The field bit tells C# that the attribute targets the field which is exactly what we want for the Header. The ‘field’ here is the backing field created by the compiler when compiling the property. It’s beyond the scope of this post.

1 Like

Privacy & Terms