How to Select your Own Player before Spawn?

I set autoCreatePlayer to false and I run two Functions on two Gui Buttons. Each connection gets 2 options for a test.

public class SpawnPlayer : NetworkBehaviour {

    public GameObject[] playerSelected;

    [Command]
    public void CmdSyncPlayerSelection(int player)
    {
        NetworkManager.singleton.playerPrefab = playerSelected[player];
    }
}

//Separate Script, just the function.
public void SpawnAPlayer()
    {
        playerSelectPanel.gameObject.SetActive(false);
        ClientScene.AddPlayer(0);
    }

I tried adding a [SyncVar] attribute to ‘public GameObject[ ] playerSelected;’, but the console told me to try SyncLists. I’ll take time to figure this lead out, but am I on the right track? This code didn’t work without the [SyncVar] either.

What’s happening is that I can choose the player on the Host, but the remote client’s player when spawned is identical to the Host’s (no matter what gui button I choose). So I ran a build as host and used the editor as a remote client. After selecting the opposite player on the remote client, it displayed the correct gameObject in the playerPrefab variable in the NetworkManager. Yet it still spawns the wrong gameObject. Very frustrating.

I’m running this script on the PlayerSelectionPanel, perhaps I can spawn an empty in case I absolutely need a player object to run [command]. Both gameObjects are inside the Spawnable Objects list in the Net Manager.

NetworkManager.playerPrefab is not replicated or intended to be used for individual player prefabs. You need to modify the OnServerAddPlayer code to use the playerSelected array instead. Its also probably a good idea to work out the player id on the server side instead of the client passing it since the server will have to work it out anyway in OnServerAddPlayer, probably using something in the connection (i.e.NetworkBehaviour.connectionToClient.connectionId)

Thanks very much for the tip Oshroth. I’ll monkey around with this for another day or two and get back to you on where I end up.