Search Unity

Spawning player issues using SpawnAsPlayerObject

Discussion in 'Netcode for GameObjects' started by ClearDark, May 11, 2022.

  1. ClearDark

    ClearDark

    Joined:
    Aug 25, 2013
    Posts:
    14
    Hello,

    I'm trying to control the flow of the spawning in my game. I have enabled Approval Check from the NetworkManager object in the editor, and on my approval check function I try to spawn the player as instructed.

    This is the code I use:

    Code (CSharp):
    1.     private void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager.ConnectionApprovedDelegate connectionApprovedCallback)
    2.     {
    3.         connectionApprovedCallback(false, null, true, Vector3.zero, Quaternion.identity);
    4.         GameObject newPlayer;
    5.         newPlayer = (GameObject)Instantiate(PlayerPrefab);
    6.         var netObj = newPlayer.GetComponent<NetworkObject>();
    7.         netObj.SpawnAsPlayerObject(clientId);
    8.     }

    When starting a server as Host, the game loads and the player prefab loads fine. When another client attempts to join, the host can see the client, however on the client instance, the player prefab is destroyed instantly upon spawning and the client returns to the main camera.

    Am I missing something here? Approval check runs only on the server, I can see the player character for a split second on the client.

    EDIT: When changing the approval check function to only this:
    Code (CSharp):
    1.     private void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager.ConnectionApprovedDelegate connectionApprovedCallback)
    2.     {
    3.         connectionApprovedCallback(true, null, true, Vector3.zero, Quaternion.identity);
    4.  
    5.     }
    Everything works as expected.




    Thanks for any help!
     
    Last edited: May 11, 2022