Client can't join the server. Prefab not found. Changing scenes problem.

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:
9152309--1272467--upload_2023-7-17_18-43-45.png

And the start network script is on a canvas.

Console after client press start client and then start game:

Hi @Eigulite ,
If you want to work with multiple scene at NGO, you should use use “NetworkManager.Singleton.SceneManager” instead of Unity “SceneManager”.
Have you ever read this page?

I’m making so that everyone would be in the same scene. If a player wants to join they’ll join the scene and leave whenever they want.

Problem fixed thanks

1 Like