Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

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

Discussion in 'Netcode for GameObjects' started by Eigulite, Jul 17, 2023.

  1. Eigulite

    Eigulite

    Joined:
    May 13, 2022
    Posts:
    60
    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:

    Code (CSharp):
    1. public class Spawner : NetworkBehaviour
    2. {
    3.     [SerializeField] private GameObject playerPrefab;
    4.  
    5.     void Start()
    6.     {
    7.         DontDestroyOnLoad(gameObject);
    8.     }
    9.  
    10.     private void OnEnable()
    11.     {
    12.         SceneManager.sceneLoaded += OnSceneLoaded;
    13.     }
    14.  
    15.     private void OnDisable()
    16.     {
    17.         SceneManager.sceneLoaded -= OnSceneLoaded;
    18.     }
    19.  
    20.     void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    21.     {
    22.         // this is called on the client
    23.         if (scene.name == "InGame")
    24.         {
    25.             if (IsClient)
    26.             {
    27.                 // this is called on the client
    28.                 // we need to tell the server that we joined the scene
    29.                 PlayerJoinedSceneServerRPC();
    30.             }
    31.         }
    32.     }
    33.  
    34.     [ServerRpc(RequireOwnership = false)]
    35.     public void PlayerJoinedSceneServerRPC(ServerRpcParams serverRpcParams = default)
    36.     {
    37.         // this is called on the server
    38.         ulong id = serverRpcParams.Receive.SenderClientId;
    39.  
    40.         GameObject player = Instantiate(playerPrefab);
    41.         player.GetComponent<NetworkObject>().SpawnAsPlayerObject(id, true);
    42.         // since the prefab is a NetworkObject, it will be spawned on the clients automatically (hopefully)
    43.     }
    44. }
    Network start code:

    Code (CSharp):
    1. public class StartNetwork : MonoBehaviour
    2. {
    3.     public void StartHost()
    4.     {
    5.         StartCoroutine(StartHostScene());
    6.     }
    7.  
    8.     public void StartClient()
    9.     {
    10.         StartCoroutine(StartClientScene());
    11.     }
    12.  
    13.     public void StartGame()
    14.     {
    15.         StartCoroutine(StartGameScene());
    16.     }
    17.  
    18.     IEnumerator StartHostScene()
    19.     {
    20.         yield return new WaitUntil(() => NetworkManager.Singleton != null);
    21.         NetworkManager.Singleton.StartHost();
    22.     }
    23.  
    24.     IEnumerator StartClientScene()
    25.     {
    26.         yield return new WaitUntil(() => NetworkManager.Singleton != null);
    27.         NetworkManager.Singleton.StartClient();
    28.     }
    29.  
    30.     IEnumerator StartGameScene()
    31.     {
    32.         yield return new WaitUntil(() => NetworkManager.Singleton != null);
    33.         SceneManager.LoadScene("InGame", LoadSceneMode.Single);
    34.     }
    35. }
    36.  
    Network manager:
    upload_2023-7-17_18-42-5.png

    Spawner object:
    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:
    upload_2023-7-17_18-37-46.png
     
  2. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    200
    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?
     
  3. Eigulite

    Eigulite

    Joined:
    May 13, 2022
    Posts:
    60
    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.
     
  4. Eigulite

    Eigulite

    Joined:
    May 13, 2022
    Posts:
    60
    Problem fixed thanks
     
    Mj-Kkaya likes this.