Consider this Unity C# code using Netcode for GameObjects. What will be printed in the console when the server calls SpawnObject()?
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"); } } }
Remember that IsServer is true only on the server instance.
The method SpawnObject() checks if the current instance is the server. If yes, it instantiates the prefab and calls Spawn() on its NetworkObject component, then logs "Object spawned on server". On clients, it logs "Not server, cannot spawn". So when called on the server, the output is "Object spawned on server".
In Unity Netcode for GameObjects, which component is responsible for managing the connection and communication between clients and the server?
Think about the component that controls starting and stopping the network session.
The NetworkManager is the central component that manages client-server connections, session lifecycle, and message routing. Other components like NetworkObject represent networked entities, but do not manage communication.
Look at this code snippet. The health variable is a NetworkVariable but clients never see its updated value. What is the cause?
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; } }
Check where TakeDamage is called and what the IsServer check does.
The TakeDamage method only updates health if IsServer is true. If this method is called on a client, it returns early and does not update the variable. So if damage is applied on clients, the server never updates the value and clients don't see changes.
Choose the correct way to define a ServerRpc method that clients can call to request a jump action.
ServerRpc methods must be public and can have parameters.
Option D correctly defines a public ServerRpc method with a parameter. Option D is valid syntax (empty parentheses are present). Option D is private, which is invalid. Option D has extra unrelated method but that does not affect ServerRpc correctness.
Given this code running on the server, how many NetworkObjects will be spawned in the scene?
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(); } }
Count all calls to Spawn() inside the method.
The loop runs 3 times, spawning 3 objects. Then one more object is spawned after the loop. Total spawned NetworkObjects are 4.