0
0
Unityframework~10 mins

Public vs SerializeField in Unity - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Public vs SerializeField
Declare Variable
Is it Public?
YesVisible in Inspector & Accessible Everywhere
No
Has SerializeField?
YesVisible in Inspector, Private Access
No
Not Visible in Inspector, Private Access
This flow shows how variable visibility and access depend on being public or marked with SerializeField in Unity.
Execution Sample
Unity
public class Player : MonoBehaviour
{
    public int health = 100;
    [SerializeField] private int mana = 50;
    private int stamina = 75;
}
Defines three variables with different access and inspector visibility in Unity.
Execution Table
StepVariableDeclarationInspector VisibleAccess LevelNotes
1healthpublic int health = 100;YesPublicVisible in Inspector and accessible anywhere
2mana[SerializeField] private int mana = 50;YesPrivateVisible in Inspector but not accessible outside class
3staminaprivate int stamina = 75;NoPrivateNot visible in Inspector and not accessible outside class
4End---All variables declared with their visibility and access
💡 All variables processed with their visibility and access determined by public or SerializeField.
Variable Tracker
VariableDeclaredInspector VisibleAccess Level
healthYesYesPublic
manaYesYesPrivate
staminaYesNoPrivate
Key Moments - 3 Insights
Why does a private variable with [SerializeField] show in the Inspector but is not accessible from other scripts?
Because [SerializeField] tells Unity to show the variable in the Inspector for editing, but the private keyword keeps it hidden from other scripts, as shown in execution_table rows 2 and 3.
If a variable is public, does it always show in the Inspector?
Yes, public variables are always visible in the Inspector by default, as seen in execution_table row 1.
What happens if a variable is private and not marked with [SerializeField]?
It will not show in the Inspector and cannot be accessed outside the class, as shown in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which variable is visible in the Inspector but not accessible outside its class?
Ahealth
Bmana
Cstamina
DNone
💡 Hint
Check the 'Inspector Visible' and 'Access Level' columns in rows 2 and 3.
At which step does the variable have public access and is visible in the Inspector?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Access Level' and 'Inspector Visible' columns in the execution_table.
If we remove [SerializeField] from mana, what changes in the variable_tracker?
Amana becomes accessible outside class
Bmana becomes public
Cmana is no longer visible in Inspector
DNo change
💡 Hint
Refer to the 'Inspector Visible' column for mana in variable_tracker.
Concept Snapshot
Public vs SerializeField in Unity:
- public variables: visible in Inspector, accessible everywhere
- private variables: hidden in Inspector, accessible only inside class
- [SerializeField] private: visible in Inspector, but private access
Use [SerializeField] to keep variables private but editable in Inspector.
Full Transcript
In Unity, variables can be public or private. Public variables show in the Inspector and can be accessed by any script. Private variables are hidden in the Inspector and only accessible inside their class. Using [SerializeField] on a private variable makes it visible in the Inspector but keeps it private in code. This lets you edit values in the Unity Editor without exposing them to other scripts. The execution table shows three variables: health is public and visible everywhere; mana is private but serialized, so visible in Inspector but not accessible outside; stamina is private and not serialized, so hidden and private. This helps organize your code and control what other scripts can change.