Force Unity to serialize a private field.
When Unity serializes your scripts, it only serializes public fields.
If you also want Unity to serialize your private fields
you can add the SerializeField attribute to those fields.
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.
The serialization system can do the following:
Serializable types
Unity can serialize fields of the following types:
For more information on serialization, see Script Serialization.
Note: If you put one element in a list (or array) twice, when the
list gets serialized, you'll get two copies of that element, instead of one copy being in the new list twice.
Note: If you want to serialize a custom Struct field, you must give the Struct the [System.Serializable] attribute.
Hint: Unity won't serialize Dictionary, however you could store a List<> for
keys and a List<> for values. See ISerializationCallbackReceiver for an example.
See Also: NonSerialized, SerializeReference, Serializable
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); } }