The host can join but the client can’t. When you try to connect as a client to the server you can see the host in the clients pov. The clients prefab doesn’t spawn but the host does.
Player spawner code:
public class Spawner : NetworkBehaviour
{
[SerializeField] private GameObject playerPrefab;
void Start()
{
DontDestroyOnLoad(gameObject);
}
private void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
private void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
// this is called on the client
if (scene.name == "InGame")
{
if (IsClient)
{
// this is called on the client
// we need to tell the server that we joined the scene
PlayerJoinedSceneServerRPC();
}
}
}
[ServerRpc(RequireOwnership = false)]
public void PlayerJoinedSceneServerRPC(ServerRpcParams serverRpcParams = default)
{
// this is called on the server
ulong id = serverRpcParams.Receive.SenderClientId;
GameObject player = Instantiate(playerPrefab);
player.GetComponent<NetworkObject>().SpawnAsPlayerObject(id, true);
// since the prefab is a NetworkObject, it will be spawned on the clients automatically (hopefully)
}
}
Network start code:
public class StartNetwork : MonoBehaviour
{
public void StartHost()
{
StartCoroutine(StartHostScene());
}
public void StartClient()
{
StartCoroutine(StartClientScene());
}
public void StartGame()
{
StartCoroutine(StartGameScene());
}
IEnumerator StartHostScene()
{
yield return new WaitUntil(() => NetworkManager.Singleton != null);
NetworkManager.Singleton.StartHost();
}
IEnumerator StartClientScene()
{
yield return new WaitUntil(() => NetworkManager.Singleton != null);
NetworkManager.Singleton.StartClient();
}
IEnumerator StartGameScene()
{
yield return new WaitUntil(() => NetworkManager.Singleton != null);
SceneManager.LoadScene("InGame", LoadSceneMode.Single);
}
}
Network manager:
Spawner object:
And the start network script is on a canvas.
Console after client press start client and then start game: