0
0
Unityframework~10 mins

Variables and serialization in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Variables and serialization
Declare variable
Assign value
Serialize variable
Save to file or memory
Deserialize variable
Use restored value
End
This flow shows how a variable is declared, given a value, saved (serialized), then loaded back (deserialized) for use.
Execution Sample
Unity
using UnityEngine;

public class Example : MonoBehaviour {
    [SerializeField] private int score = 10;
}
This code declares a private variable 'score' with initial value 10 and marks it to be serialized by Unity.
Execution Table
StepActionVariableValueSerialization State
1Declare variable 'score'scoreundefinedNot serialized yet
2Assign initial valuescore10Not serialized yet
3Serialize variablescore10Serialized to Unity Editor and saved
4Save to file/memoryscore10Stored in scene or prefab data
5Deserialize variablescore10Loaded back from saved data
6Use variable in gamescore10Active in runtime
7End---
💡 Serialization completes when variable is saved and loaded back correctly.
Variable Tracker
VariableStartAfter AssignAfter SerializeAfter DeserializeFinal
scoreundefined10101010
Key Moments - 3 Insights
Why do we use [SerializeField] on a private variable?
Because private variables are hidden from Unity's Inspector by default, [SerializeField] tells Unity to save and show it anyway, as shown in step 3 of the execution_table.
Does serialization change the variable's value?
No, serialization saves the current value without changing it. The value remains 10 before and after serialization (see variable_tracker).
What happens if we don't serialize a variable?
Unity won't save its value between sessions, so it resets to default or code value on reload. This is why serialization is important (step 3 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'score' after step 2?
A0
Bundefined
C10
Dnull
💡 Hint
Check the 'Value' column at step 2 in the execution_table.
At which step does the variable 'score' get saved to file or memory?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the action mentioning saving to file or memory in the execution_table.
If we remove [SerializeField], what happens to the variable 'score' in Unity Inspector?
AIt becomes invisible and not serialized
BIt causes a compile error
CIt remains visible and serialized
DIt becomes public automatically
💡 Hint
Refer to the key_moments about [SerializeField] usage.
Concept Snapshot
Variables hold data in Unity scripts.
[SerializeField] makes private variables visible and saved.
Serialization saves variable values to disk or memory.
Deserialization restores saved values when loading.
Without serialization, values reset on reload.
Full Transcript
This lesson shows how variables in Unity scripts can be saved and loaded using serialization. We start by declaring a variable called 'score' and assign it the value 10. Using the [SerializeField] attribute, Unity knows to save this private variable's value. Serialization means saving the variable's current value to a file or memory so it can be restored later. When the game or editor reloads, Unity deserializes the variable, loading back the saved value. This way, the variable keeps its value between sessions. Without serialization, private variables would reset to default values and not keep changes made in the editor or during gameplay. The execution table traces each step from declaration, assignment, serialization, saving, deserialization, to usage. The variable tracker shows the value stays 10 throughout. Key moments clarify why [SerializeField] is needed and that serialization does not change the value. The quiz tests understanding of variable values at steps and the effect of serialization attributes.