This version of Unity is unsupported.

Serializable

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.

The following example creates a custom PlayerStats struct and gives it the [Serializable] attribute, which makes it serializable. It then creates a private field of type PlayerStats, and applies the [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; }