Bird
Raised Fist0
Unityframework~10 mins

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

Choose your learning style10 modes available
Concept Flow - Public vs SerializeField
Declare Variable
↓
Is it Public?
Yes→Visible in Inspector & Accessible Everywhere
No↓
Has SerializeField?
Yes→Visible 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.

Practice

(1/5)
1. What is the main difference between a public variable and a [SerializeField] private variable in Unity?
easy
A. public variables 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.
B. public variables are hidden in the Inspector, but [SerializeField] private variables are accessible by all scripts.
C. public variables cannot be edited in the Inspector, but [SerializeField] private variables can be edited by other scripts.
D. public variables are only accessible inside the class, while [SerializeField] private variables are accessible everywhere.

Solution

  1. Step 1: Understand public variable behavior

    A public variable is accessible by all scripts and also visible in the Unity Inspector for editing.
  2. Step 2: Understand [SerializeField] with private

    A [SerializeField] private variable is hidden from other scripts but still visible and editable in the Inspector.
  3. Final Answer:

    public variables 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 A
  4. Quick Check:

    Access and visibility differ between public and [SerializeField] private [OK]
Hint: Public = accessible everywhere; SerializeField private = Inspector only [OK]
Common Mistakes:
  • Thinking SerializeField makes variable public
  • Assuming public variables are hidden in Inspector
  • Confusing private and public accessibility
2. Which of the following is the correct way to make a private variable visible in the Unity Inspector?
easy
A. private int score; [SerializeField]
B. [SerializeField] private int score;
C. public [SerializeField] int score;
D. int [SerializeField] private score;

Solution

  1. Step 1: Recall correct attribute placement

    The [SerializeField] attribute must be placed before the variable declaration.
  2. Step 2: Check variable declaration syntax

    The correct syntax is [SerializeField] private int score; to make a private variable visible in Inspector.
  3. Final Answer:

    [SerializeField] private int score; -> Option B
  4. Quick Check:

    Attribute before private variable declaration [OK]
Hint: Put [SerializeField] before private variable declaration [OK]
Common Mistakes:
  • Placing [SerializeField] after variable
  • Mixing public and [SerializeField] incorrectly
  • Wrong order of keywords
3. Consider this Unity C# script snippet:
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?
medium
A. Error: health is private and cannot be accessed
B. 0 and 100
C. 100 and 0
D. Nothing will print

Solution

  1. Step 1: Understand variable values and accessibility

    The private variable health is serialized and initialized to 100, so it is accessible inside the class and prints 100. The public variable score is initialized to 0 and prints 0.
  2. Step 2: Check Debug.Log outputs

    Both Debug.Log(health) and Debug.Log(score) print their current values: 100 and 0 respectively.
  3. Final Answer:

    100 and 0 -> Option C
  4. Quick Check:

    Private serialized variables accessible inside class [OK]
Hint: Private serialized variables print normally inside class [OK]
Common Mistakes:
  • Thinking private variables can't be accessed inside class
  • Confusing initialization values
  • Assuming serialization affects runtime access
4. You have this code in Unity:
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?
medium
A. The damage variable is private and cannot be changed at runtime.
B. The [SerializeField] attribute is only for public variables.
C. You forgot to make damage public to change it in code.
D. The Inspector value overrides the code assignment, so damage stays at 0.

Solution

  1. 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.
  2. Step 2: Explain why damage stays 0

    The Inspector value for damage is 0, so even though code initializes it to 10, the Inspector value overrides it.
  3. Final Answer:

    The Inspector value overrides the code assignment, so damage stays at 0. -> Option D
  4. Quick Check:

    Inspector values override code assignments at runtime [OK]
Hint: Inspector values override code assignments on serialized fields [OK]
Common Mistakes:
  • Assuming private variables can't be changed in code
  • Thinking [SerializeField] only works on public variables
  • Ignoring Inspector value overrides
5. You want to keep a variable speed editable in the Unity Inspector but prevent other scripts from modifying it directly. Which is the best way to declare it?
hard
A. [SerializeField] private float speed;
B. private float speed; // no attribute
C. public float speed;
D. public float speed { get; private set; }

Solution

  1. Step 1: Identify requirement for Inspector editing and script protection

    The variable must be editable in Inspector but not accessible publicly by other scripts.
  2. 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] with private, 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.
  3. Final Answer:

    [SerializeField] private float speed; -> Option A
  4. Quick Check:

    SerializeField + private = Inspector editable, script protected [OK]
Hint: Use [SerializeField] private for Inspector edit, no public access [OK]
Common Mistakes:
  • Making variable public and exposing it
  • Using property without SerializeField
  • Forgetting to add [SerializeField]
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard