SerializeField
Force Unity to serialize a private field.
Read time 4 minutesLast updated 4 days ago
Definition
- Type: Class
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
- Inherits from: Attribute
- Implements: _Attribute
public sealed class SerializeField : Attribute, _Attribute
Remarks
Unity only serializes public fields by default. To serialize private fields, add the attribute to them.
[SerializeField]Unity serializes all your script components, reloads the new assemblies, and recreates your script components from the serialized versions. This serialization is done with an internal Unity serialization system; not with .NET's serialization functionality.
For a full reference of what Unity can serialize, refer to Serialization rules.
Additional Resources: SerializeReference
Examples
using UnityEngine;public class SomePerson : MonoBehaviour{ //This field gets serialized because it is public. public string firstName = "John"; //This field does not get serialized because it is private. private int age = 40; //This field gets serialized even though it is private //because it has the SerializeField attribute applied. [SerializeField] private bool hasHealthPotion = true; void Start() { if (hasHealthPotion) Debug.Log("Person's first name: " + firstName + " Person's age: " + age); }}