Challenge - 5 Problems
Unity Serialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Unity C# code snippet?
Consider the following Unity C# script attached to a GameObject. What will be printed in the console when the game starts?
Unity
using UnityEngine; public class TestSerialization : MonoBehaviour { [SerializeField] private int score = 10; void Start() { score += 5; Debug.Log(score); } }
Attempts:
2 left
💡 Hint
Remember that [SerializeField] allows private variables to be set and used in the inspector and code.
✗ Incorrect
The private variable 'score' is initialized to 10. In Start(), 5 is added, so the output is 15.
❓ Predict Output
intermediate2:00remaining
What happens when serializing a non-serializable field in Unity?
Given this Unity C# class, what will happen when you try to serialize it in the inspector?
Unity
using UnityEngine;
public class PlayerData : MonoBehaviour
{
[SerializeField]
private System.Net.Sockets.Socket playerSocket;
}Attempts:
2 left
💡 Hint
Unity can only serialize certain types like primitives, UnityEngine.Object, and serializable structs/classes.
✗ Incorrect
Unity ignores fields of types it cannot serialize, so playerSocket is not shown or saved in the inspector.
🔧 Debug
advanced2:00remaining
Why does this serialized field not retain its value after stopping play mode?
Examine the code below. The 'health' value changes during play but resets after stopping play mode. Why?
Unity
using UnityEngine; public class PlayerStats : MonoBehaviour { [SerializeField] private int health = 100; void Update() { if (Input.GetKeyDown(KeyCode.Space)) { health -= 10; Debug.Log(health); } } }
Attempts:
2 left
💡 Hint
Think about how Unity handles serialized fields and play mode changes.
✗ Incorrect
Unity does not save changes made to serialized fields during play mode after stopping play; the original serialized value is restored.
📝 Syntax
advanced2:00remaining
Which option correctly declares a serializable custom class in Unity?
You want to serialize a custom class inside a MonoBehaviour. Which declaration is correct?
Attempts:
2 left
💡 Hint
Custom classes must be marked with [System.Serializable] to be serialized by Unity.
✗ Incorrect
Only option C correctly marks the class with [System.Serializable]. [SerializeField] cannot be applied to classes.
🧠 Conceptual
expert2:00remaining
What is the main reason Unity uses serialization for variables in scripts?
Why does Unity serialize variables marked with [SerializeField] or public fields in scripts?
Attempts:
2 left
💡 Hint
Think about what happens when you save a scene or prefab in Unity.
✗ Incorrect
Serialization allows Unity to save variable values so they persist in the editor and when loading scenes or prefabs.