What is SerializeField in Unity?

What is SerializeField in Unity?

While working in Unity, you may often come across a keyword called SerializeField. You might also hear phrases like, "This variable needs to be serialized to show up in the Inspector." Although it may sound unfamiliar at first, the concept of serialization is surprisingly simple and essential for understanding Unity’s behavior.

What is Serialization?

Serialization refers to the process of laying out data in a linear, ordered format. While objects in code may appear structured to humans, computers, file systems, and networks cannot interpret them as-is.

Serialization transforms the internal data of an object into a continuous format, making it suitable for storage or transmission.

  • Turning data into a sequence → Serialization
  • Reconstructing the original object from that sequence → Deserialization

Why Is Serialization Needed in Unity?

In Unity, serialization is necessary to display variable values in the Inspector. If a variable is not serialized, it will not appear in the Inspector, nor will its value be saved.

If you want to tweak values in the Inspector or maintain them when saving scenes, the variable must be serialized.

What Kind of Variables Are Serialized?

The following types of variables are serialized and shown in the Unity Inspector:

  • public variables are automatically serialized.
  • private variables can be serialized by adding the [SerializeField] attribute.
  • The variable type must also be serializable.

Examples of serializable types: int, float, string, Vector3, Color, Transform, etc.
For custom classes or structs, you must mark them with [System.Serializable].

Example

public class PlayerData : MonoBehaviour
{
    // ✅ Shown in the Inspector
    public int health = 100;

    // ✅ Shown in the Inspector
    [SerializeField]
    private float speed = 5.0f;

    // ❌ Not shown in the Inspector
    private bool isDead = false;
}
  • health is public, so it is serialized automatically.
  • speed is private, but the [SerializeField] attribute makes it visible and serialized.
  • isDead is private and lacks serialization, so it won't appear in the Inspector.

Summary

  • Serialization arranges data for saving or transferring.
  • Unity requires serialization for variables to be visible in the Inspector.
  • SerializeField allows you to serialize private variables for Inspector visibility.

Additional Notes

In Unity, once a value is stored in the Inspector, any initial value assigned in the script (like = 100) is ignored. So, is it unnecessary to assign default values in code?

Not necessarily. Setting default values in the script is still useful:

  • They are applied the first time the component is added in the Editor.

In short, script-side initialization is useful for setting a starting point, while most value changes and storage are handled through the Inspector afterward.

Popular posts from this blog

Understanding Arrays as Reference Types in C#

Setting Up a Basic Follow Camera with Cinemachine 3.x

Understanding and Using ? (nullable) in C#