0
0
Unityframework~20 mins

Unity Netcode overview - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unity Netcode Mastery
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 Netcode snippet?

Consider this Unity C# code using Netcode for GameObjects. What will be printed in the console when the server calls SpawnObject()?

Unity
using Unity.Netcode;
using UnityEngine;

public class NetworkSpawner : NetworkBehaviour
{
    public GameObject prefab;

    public void SpawnObject()
    {
        if (IsServer)
        {
            GameObject obj = Instantiate(prefab);
            obj.GetComponent<NetworkObject>().Spawn();
            Debug.Log("Object spawned on server");
        }
        else
        {
            Debug.Log("Not server, cannot spawn");
        }
    }
}
AObject spawned on server
BNot server, cannot spawn
CNullReferenceException at Instantiate
DCompile error due to missing NetworkObject
Attempts:
2 left
💡 Hint

Remember that IsServer is true only on the server instance.

🧠 Conceptual
intermediate
1:30remaining
Which Unity Netcode component manages client-server communication?

In Unity Netcode for GameObjects, which component is responsible for managing the connection and communication between clients and the server?

ANetworkObject
BNetworkManager
CNetworkTransform
DNetworkAnimator
Attempts:
2 left
💡 Hint

Think about the component that controls starting and stopping the network session.

🔧 Debug
advanced
2:30remaining
Why does this NetworkVariable not sync to clients?

Look at this code snippet. The health variable is a NetworkVariable but clients never see its updated value. What is the cause?

Unity
using Unity.Netcode;

public class PlayerHealth : NetworkBehaviour
{
    public NetworkVariable<int> health = new NetworkVariable<int>(100);

    public void TakeDamage(int amount)
    {
        if (!IsServer) return;
        health.Value -= amount;
    }
}
AThe TakeDamage method is not called on the server
BThe NetworkVariable is not marked as public
CThe NetworkVariable is not initialized with a default value
DThe health variable is not synchronized because it lacks a NetworkObject
Attempts:
2 left
💡 Hint

Check where TakeDamage is called and what the IsServer check does.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly defines a ServerRpc in Unity Netcode?

Choose the correct way to define a ServerRpc method that clients can call to request a jump action.

A
[ServerRpc]
public void RequestJump()
{
    // jump logic
}
B
[ServerRpc]
private void RequestJump()
{
    // jump logic
}
C
[ServerRpc]
public void RequestJump()
{
    // jump logic
}

public void Jump() {}
D
[ServerRpc]
public void RequestJump(int height)
{
    // jump logic
}
Attempts:
2 left
💡 Hint

ServerRpc methods must be public and can have parameters.

🚀 Application
expert
3:00remaining
How many NetworkObjects are spawned after this code runs on the server?

Given this code running on the server, how many NetworkObjects will be spawned in the scene?

Unity
using Unity.Netcode;
using UnityEngine;

public class Spawner : NetworkBehaviour
{
    public GameObject prefab;

    public void SpawnMultiple()
    {
        if (!IsServer) return;

        for (int i = 0; i < 3; i++)
        {
            GameObject obj = Instantiate(prefab);
            obj.GetComponent<NetworkObject>().Spawn();
        }

        GameObject single = Instantiate(prefab);
        single.GetComponent<NetworkObject>().Spawn();
    }
}
A3
B1
C4
D0
Attempts:
2 left
💡 Hint

Count all calls to Spawn() inside the method.