The SerializeField attribute is redundant for public fields, and invalid for properties or static/readonly fields. Unlike SerializeReference, the compiler allows you to use the SerializeField attribute on properties, even if it is invalid and will not function in the Unity editor.
using System.Collections;
using UnityEngine;
public class SerializedAttributes : MonoBehaviour
{
[SerializeField] // correct usage
private string privateField;
[SerializeField] // redundant usage
public string publicField;
[SerializeField] // invalid usage
private string PrivateProperty { get; set; }
[SerializeField] // invalid usage
static string staticField;
[SerializeField] // invalid usage
readonly field readonlyField;
}
Remove the SerializeField attribute when it is redundant or invalid:
using System.Collections;
using UnityEngine;
public class SerializedAttributes : MonoBehaviour
{
[SerializeField] // correct usage
private string privateField;
public string publicField;
private string PrivateProperty { get; set; }
static string staticField;
readonly field readonlyField;
}
A code fix is offered for this diagnostic to automatically apply this change.