0
0
Unityframework~20 mins

Variables and serialization in Unity - Practice Problems & Coding Challenges

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
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);
    }
}
A5
B15
C10
D0
Attempts:
2 left
💡 Hint
Remember that [SerializeField] allows private variables to be set and used in the inspector and code.
Predict Output
intermediate
2: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;
}
AThe inspector ignores the playerSocket field; no error occurs.
BThe inspector shows the playerSocket field and allows editing.
CUnity throws a serialization error at compile time.
DThe game crashes at runtime.
Attempts:
2 left
💡 Hint
Unity can only serialize certain types like primitives, UnityEngine.Object, and serializable structs/classes.
🔧 Debug
advanced
2: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);
        }
    }
}
ABecause 'health' is private and not marked as [Serializable].
BBecause the Update method resets health every frame.
CBecause the script is missing a constructor to initialize health.
DBecause changes made during play mode are not saved to the serialized field.
Attempts:
2 left
💡 Hint
Think about how Unity handles serialized fields and play mode changes.
📝 Syntax
advanced
2: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?
A[SerializeField] public class Weapon { public string name; public int damage; }
Bpublic class Weapon { [SerializeField] public string name; public int damage; }
C[System.Serializable] public class Weapon { public string name; public int damage; }
Dpublic struct Weapon { string name; int damage; }
Attempts:
2 left
💡 Hint
Custom classes must be marked with [System.Serializable] to be serialized by Unity.
🧠 Conceptual
expert
2: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?
ATo save and load variable values in the editor and during scene serialization.
BTo improve runtime performance by caching variables.
CTo prevent variables from being changed by other scripts.
DTo automatically convert variables to JSON for networking.
Attempts:
2 left
💡 Hint
Think about what happens when you save a scene or prefab in Unity.