Version: 2021.2
LanguageEnglish
  • C#

Serializable

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Indicates that a class or a struct can be serialized.

To enable serialization, apply the [Serializable] attribute. For more information on serialization, see Script Serialization.

Note: Only non-abstract, non-generic custom classes can be serialized.

In the following example we create a custom Player struct and give it the [Serializable] attribute to make it serializable. We then create a private field of the Player type and apply [SerializeField] attribute to it to make it show up in the Inspector.

using System;
using UnityEngine;

public class Player : MonoBehaviour { //Create a custom struct and apply [Serializable] attribute to it [Serializable] public struct PlayerStats { public int movementSpeed; public int hitPoints; public bool hasHealthPotion; }

//Make the private field of our PlayerStats struct visible in the Inspector //by applying [SerializeField] attribute to it [SerializeField] private PlayerStats stats; }