Force Unity to serialize a private field.
You will almost never need this. When Unity serializes your scripts, it will only serialize public fields. If in addition to that you also want Unity to serialize one of your private fields you can add the SerializeField attribute to the field.
Unity will serialize all your script components, reload the new assemblies, and recreate your script components from the serialized verions. This serialization does not happen with .NET's serialization functionality, but with an internal Unity one.The serialization system used can do the following:- CAN serialize public nonstatic fields (of serializable types)// Javascript example//This field gets serialized because it is public. var name = "John";//This field does not get serialized because it is private. private var age = 40;//This field gets serialized even though it is private //because it has the SerializeField attribute applied. @SerializeField private var hasHealthPotion:boolean = true;function Update () { }
//C# example using UnityEngine;public class SomePerson : MonoBehaviour { //This field gets serialized because it is public. public string name = "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 Update () { } }