We use public and [SerializeField] to control how variables show up and can be changed in Unity's Inspector. This helps us organize and protect our game data.
Public vs SerializeField in Unity
public int myNumber; [SerializeField] private int myNumber;
public makes the variable visible to all scripts and the Inspector.
[SerializeField] makes a private variable visible in the Inspector but hidden from other scripts.
public int score; // score is visible and can be changed by other scripts and in Inspector
[SerializeField] private int health; // health is private but visible and editable in Inspector
private int speed; // speed is hidden from Inspector and other scripts
This script shows three variables with different visibility. You can see and change publicScore and privateHealth in the Inspector. hiddenSpeed is hidden. The Start method prints their values.
using UnityEngine; public class Player : MonoBehaviour { public int publicScore = 10; // visible to all and Inspector [SerializeField] private int privateHealth = 100; // private but visible in Inspector private int hiddenSpeed = 5; // private and hidden in Inspector void Start() { Debug.Log($"Public Score: {publicScore}"); Debug.Log($"Private Health: {privateHealth}"); Debug.Log($"Hidden Speed: {hiddenSpeed}"); } }
Use public only when you want other scripts to access the variable.
[SerializeField] is great for keeping variables private but still editable in the Inspector.
Keeping variables private helps protect your data and avoid accidental changes from other scripts.
public variables are visible to all scripts and the Inspector.
[SerializeField] makes private variables visible in the Inspector only.
Use private with [SerializeField] to keep data safe but editable in Unity Editor.
Practice
public variable and a [SerializeField] private variable in Unity?Solution
Step 1: Understand
Apublicvariable behaviorpublicvariable is accessible by all scripts and also visible in the Unity Inspector for editing.Step 2: Understand
A[SerializeField]withprivate[SerializeField]private variable is hidden from other scripts but still visible and editable in the Inspector.Final Answer:
publicvariables are accessible by other scripts and visible in the Inspector, while[SerializeField]private variables are only visible in the Inspector but not accessible by other scripts. -> Option AQuick Check:
Access and visibility differ betweenpublicand[SerializeField]private [OK]
- Thinking SerializeField makes variable public
- Assuming public variables are hidden in Inspector
- Confusing private and public accessibility
Solution
Step 1: Recall correct attribute placement
The[SerializeField]attribute must be placed before the variable declaration.Step 2: Check variable declaration syntax
The correct syntax is[SerializeField] private int score;to make a private variable visible in Inspector.Final Answer:
[SerializeField] private int score; -> Option BQuick Check:
Attribute before private variable declaration [OK]
- Placing [SerializeField] after variable
- Mixing public and [SerializeField] incorrectly
- Wrong order of keywords
public class Player : MonoBehaviour {
[SerializeField] private int health = 100;
public int score = 0;
void Start() {
Debug.Log(health);
Debug.Log(score);
}
}
What will be printed in the Unity Console when the game starts?Solution
Step 1: Understand variable values and accessibility
The private variablehealthis serialized and initialized to 100, so it is accessible inside the class and prints 100. The public variablescoreis initialized to 0 and prints 0.Step 2: Check Debug.Log outputs
BothDebug.Log(health)andDebug.Log(score)print their current values: 100 and 0 respectively.Final Answer:
100 and 0 -> Option CQuick Check:
Private serialized variables accessible inside class [OK]
- Thinking private variables can't be accessed inside class
- Confusing initialization values
- Assuming serialization affects runtime access
public class Enemy : MonoBehaviour {
[SerializeField] private int damage = 10;
}
But in the Inspector, the damage value stays at 0 even after you press Play. What is the likely problem?Solution
Step 1: Understand Inspector and code value interaction
If a serialized field has a value set in the Inspector, Unity uses that value at runtime, overriding the code default initialization.Step 2: Explain why damage stays 0
The Inspector value fordamageis 0, so even though code initializes it to 10, the Inspector value overrides it.Final Answer:
The Inspector value overrides the code assignment, so damage stays at 0. -> Option DQuick Check:
Inspector values override code assignments at runtime [OK]
- Assuming private variables can't be changed in code
- Thinking [SerializeField] only works on public variables
- Ignoring Inspector value overrides
speed editable in the Unity Inspector but prevent other scripts from modifying it directly. Which is the best way to declare it?Solution
Step 1: Identify requirement for Inspector editing and script protection
The variable must be editable in Inspector but not accessible publicly by other scripts.Step 2: Evaluate options
public float speed; makes it public, so other scripts can modify it. private float speed; // no attribute hides it from Inspector. [SerializeField] private float speed; uses[SerializeField]withprivate, making it editable in Inspector but hidden from other scripts. public float speed { get; private set; } uses a public property with private setter, which is not visible in Inspector by default.Final Answer:
[SerializeField] private float speed; -> Option AQuick Check:
SerializeField + private = Inspector editable, script protected [OK]
- Making variable public and exposing it
- Using property without SerializeField
- Forgetting to add [SerializeField]
Ready to prove it?
Pass the test and this pattern moves unknown
