0
0
Unityframework~20 mins

Public vs SerializeField in Unity - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unity Serialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of accessing fields in Unity script

Consider this Unity C# script attached to a GameObject:

using UnityEngine;

public class TestScript : MonoBehaviour {
    public int publicValue = 5;
    [SerializeField] private int serializedValue = 10;

    void Start() {
        Debug.Log(publicValue);
        Debug.Log(serializedValue);
    }
}

What will be printed in the Unity Console when the game starts?

Unity
using UnityEngine;

public class TestScript : MonoBehaviour {
    public int publicValue = 5;
    [SerializeField] private int serializedValue = 10;

    void Start() {
        Debug.Log(publicValue);
        Debug.Log(serializedValue);
    }
}
A5 and 10
B0 and 0
C5 and 0
DCompilation error because serializedValue is private
Attempts:
2 left
💡 Hint

Remember that SerializeField makes private fields visible in the Inspector and keeps their values.

🧠 Conceptual
intermediate
1:30remaining
Why use [SerializeField] on private fields?

In Unity, why would a developer use [SerializeField] on a private field instead of making the field public?

ATo keep the field private but still editable in the Inspector
BTo make the field accessible from other scripts
CTo prevent the field from being saved with the scene
DTo make the field read-only in the Inspector
Attempts:
2 left
💡 Hint

Think about encapsulation and editing values in the Unity Editor.

🔧 Debug
advanced
2:30remaining
Why is the private field not visible in Inspector?

Look at this Unity script:

using UnityEngine;

public class Player : MonoBehaviour {
    private int health = 100;
    [SerializeField] private int mana;

    void Start() {
        Debug.Log(health);
        Debug.Log(mana);
    }
}

When attached to a GameObject, the mana field does not show in the Inspector. What is the most likely reason?

AThe <code>mana</code> field is private and missing <code>[SerializeField]</code>
BThe <code>mana</code> field is not initialized, so it won't appear
CThe script file name does not match the class name
DThe Unity Editor needs to be restarted to show private serialized fields
Attempts:
2 left
💡 Hint

Unity requires script file names to match class names exactly.

📝 Syntax
advanced
1:30remaining
Which code correctly serializes a private field?

Which of the following Unity C# code snippets correctly serializes a private integer field so it appears in the Inspector?

Aprivate int score = 0; [SerializeField]
Bpublic int score = 0; [SerializeField]
Cprivate [SerializeField] int score = 0;
D[SerializeField] private int score = 0;
Attempts:
2 left
💡 Hint

Look at the correct order of attributes and access modifiers in C#.

🚀 Application
expert
3:00remaining
Choosing field visibility for game settings

You are designing a Unity game and want to store a player's maximum health. You want this value to be editable in the Inspector but not accessible or modifiable by other scripts directly. Which declaration is best?

Apublic int maxHealth = 100;
B[SerializeField] private int maxHealth = 100;
Cprivate int maxHealth = 100;
D[SerializeField] public int maxHealth = 100;
Attempts:
2 left
💡 Hint

Think about encapsulation and Inspector visibility.