After both of the clients are connected to the lobby, the host presses the host button and this function is called
public void StartGame(){
LoadHubScene(InstanceFinder.ClientManager.Connection);
LanguageUpdate();
}
public void LoadHubScene(NetworkConnection conn){
lobbyCanvas.SetActive(false);
BroadcastLoadHub();
SceneLoadData loadSceneData = new SceneLoadData("Hub"){
ReplaceScenes = ReplaceOption.All,
};
InstanceFinder.SceneManager.LoadGlobalScenes(loadSceneData);
Debug.Log($"[Server] Transitioning connection {conn.ClientId} to Hub.");
}
Then, both of the clients are sent to the new scene, which doesn’t have any issues as far as I can tell. The problem appears when it comes to the spawning of the players. Interestingly, the chunks of the world get spawned on the server without an issue, and I only encountered this problem when it came to player spawning. Below is the GO with the script. GO SETUP
and this is the actual script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FishNet.Object;
using FishNet;
using FishNet.Object.Synchronizing;
using FishNet.Connection;
using System.Linq;
public class CustomPlayerSpawner : NetworkBehaviour
{
[SerializeField] GameObject playerGO;
[SerializeField] GameObject[] spawnPoints;
bool hasSpawned;
// Start is called before the first frame update
public override void OnStartClient()
{
base.OnStartClient();
Spawn(LocalConnection, GetRandomPositionSpawn());
}
[ServerRpc(RequireOwnership = false)]
void Spawn(NetworkConnection connection, Vector3 pos){
SpawnOnEveryClient(connection, pos);
}
[ObserversRpc(ExcludeServer = false, ExcludeOwner = false)]
void SpawnOnEveryClient(NetworkConnection connection, Vector3 pos){
Debug.Log($"Spawned for {connection}");
NetworkObject nob = InstanceFinder.NetworkManager.GetPooledInstantiated(playerGO, pos, Quaternion.identity, true);
InstanceFinder.ServerManager.Spawn(nob, connection);
nob.GiveOwnership(connection);
}
Vector3 GetRandomPositionSpawn(){
return spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position;
}
}
I was expcting for at the very least another Player GO to be spawned, with or without the other player scripts working, but even that didn’t spawn. I have tried working with different setups, including the inbuilt Fishnet spawner script, which also didn’t spawn a second gameObject.
MilknoDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.