Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
public class GameManager : MonoBehaviour
{
[SerializeField]private GameObject cube;
private GameObject SpawnedCube;
public void SpawnCube()
{
SpawnedCube = SpawnObjectServerRpc(cube, SpawnedCube);
}
[ServerRpc]
GameObject SpawnObjectServerRpc(GameObject inputSpawn, GameObject GameObjectStore)
{
if(GameObjectStore == null)
{
GameObjectStore = Instantiate(inputSpawn);
GameObjectStore.GetComponent<NetworkObject>().Spawn(true);
return GameObjectStore;
}
else
{
return null;
}
}
}
SpawnCube() function is attached to Spawn Cube button.
Error Occurs after Spawn Cube button is pressed on the client.
Host is on the left. Client is on the right.
The Error:
NotServerException: Only server can spawn NetworkObjects
Unity.Netcode.NetworkObject.SpawnInternal (System.Boolean destroyWithScene, System.UInt64 ownerClientId, System.Boolean playerObject) (at Library/PackageCache/com.unity.netcode.gameobjects@1.1.0/Runtime/Core/NetworkObject.cs:475)