The SerializeField attribute is redundant on public fields
SerializeField makes a field display in the Inspector and be saved — which declaring the field public already does. Hence the attribute is redundant on public fields.
How to fix
Remove the SerializeField attribute on matched public fields.
CQLinq source
This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.
// <Name>The SerializeField attribute is redundant on public fields</Name>
// <Id>ND3205:TheSerializeFieldAttributeIsRedundantOnPublicFields</Id>
warnif count > 0
from f in Application.Fields where
f.IsPubliclyVisible &&
f.HasAttribute("UnityEngine.SerializeFieldAttribute".AllowNoMatch())
select new {
f,
Debt = 3.ToMinutes().ToDebt(),
Severity = Severity.Medium
}
//<Expl>
// The *public {0}* shouldn't be tagged with *SerializeFieldAttribute*,
// this causes redundancy.
//</Expl>
//<Desc>
// *SerializeField* makes a field display in the Inspector and be saved —
// which declaring the field public already does. Hence the attribute is
// redundant on public fields.
//</Desc>
//<HowToFix>
// Remove the *SerializeField* attribute on matched public fields.
//</HowToFix>
Open in the interactive Rules Explorer · All default rules
API Usage / Unity: all 9 items
- Classes tagged with InitializeOnLoad should have a static constructor ND3200
- Avoid using non-generic GetComponent ND3201
- Avoid empty Unity message ND3202
- Avoid using Time.fixedDeltaTime with Update ND3203
- Use CreateInstance to create a scriptable object ND3204
- The SerializeField attribute is redundant on public fields ND3205
- InitializeOnLoadMethod should tag only static and parameterless methods ND3206
- Prefer using SetPixels32() over SetPixels() ND3207
- Don't use System.Reflection in performance critical messages ND3208