Skip to content

Latest commit

 

History

History
53 lines (36 loc) · 1.36 KB

UNT0013.md

File metadata and controls

53 lines (36 loc) · 1.36 KB

UNT0013 Invalid or redundant SerializeField attribute

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.

Examples of patterns that are flagged by this analyzer

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;
}

Solution

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.