0
0
Unityframework~20 mins

State synchronization in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
State Sync 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 state synchronization code snippet?

Consider a Unity script that synchronizes a player's health across the network using a simple RPC call. What will be the value of playerHealth on the client after the RPC call?

Unity
using UnityEngine;
using UnityEngine.Networking;

public class Player : NetworkBehaviour {
    [SyncVar]
    public int playerHealth = 100;

    [ClientRpc]
    public void RpcTakeDamage(int damage) {
        playerHealth -= damage;
        Debug.Log($"Health after damage: {playerHealth}");
    }
}

// Assume server calls RpcTakeDamage(30) on the client.
AHealth after damage: 100
BNo output, method not called on client
CHealth after damage: 130
DHealth after damage: 70
Attempts:
2 left
💡 Hint

Remember that [ClientRpc] methods run on clients and modify client state.

🧠 Conceptual
intermediate
1:30remaining
Which Unity component ensures automatic state synchronization of variables?

In Unity's networking system, which attribute or component automatically keeps a variable synchronized across server and clients?

A[SyncVar]
B[ClientRpc]
C[Command]
D[ServerOnly]
Attempts:
2 left
💡 Hint

Think about which attribute marks variables to be synced automatically.

🔧 Debug
advanced
2:30remaining
Why does this state synchronization code fail to update clients?

Examine the following Unity code snippet. Why do clients not see the updated score value?

Unity
using UnityEngine;
using UnityEngine.Networking;

public class GameManager : NetworkBehaviour {
    public int score = 0;

    [ClientRpc]
    public void RpcUpdateScore(int newScore) {
        score = newScore;
    }

    [Server]
    public void IncreaseScore() {
        score += 10;
        RpcUpdateScore(score);
    }
}
AThe variable 'score' is not marked with [SyncVar], so clients don't sync automatically.
BRpcUpdateScore is not called on the server, so clients never receive updates.
CThe score variable is private and cannot be accessed by clients.
DThe IncreaseScore method is not marked as [Command], so it cannot run on the server.
Attempts:
2 left
💡 Hint

Consider how variables synchronize automatically versus manually.

📝 Syntax
advanced
1:30remaining
Which option correctly declares a synchronized variable in Unity?

Choose the correct syntax to declare a synchronized integer variable lives that starts at 3.

Apublic SyncVar int lives = 3;
B[SyncVar] public int lives = 3;
C[SyncVar()] int lives = 3;
DSyncVar public int lives = 3;
Attempts:
2 left
💡 Hint

Remember the correct placement of attributes in C#.

🚀 Application
expert
3:00remaining
How to ensure consistent player position synchronization with interpolation?

You want to synchronize a player's position smoothly across networked clients in Unity. Which approach best achieves this?

AUse [Command] to send position from client to server and update server position only.
BSend position updates only via [ClientRpc] calls without interpolation.
CUse a [SyncVar] Vector3 for position and interpolate client-side between updates.
DSynchronize position using a local variable and update it in Update() without networking.
Attempts:
2 left
💡 Hint

Think about combining automatic sync with smooth movement on clients.