Setting individual player for many users in networking game

Hi,

For my game, I would like to allow each connected user to choose its own player prefab. I found a code snippet here: How to set individual playerPrefab form client in the NetworkManger? - Unity Engine - Unity Discussions which override the OnServerAddPlayer(…,…,MESSAGE) method.

public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId,NetworkReader extraMessageReader)
    {
        string prefabChoice = "";
        if (extraMessageReader != null)
        {
            prefabChoice  = extraMessageReader.ReadString();
        }
       
        if (prefabChoice == "prefabA")
        {
            foreach (var prefab in spawnPrefabs)
                if (prefab.tag == "prefabA")
                {
                    playerPrefab = prefab;
                    break;
                }
        }
        else if (prefabChoice == "prefabB")
        {
            foreach (var prefab in spawnPrefabs)
                if (prefab.tag == "prefabB")
                {
                    playerPrefab = prefab;
                    break;
                }
        }
        else
        {
            foreach (var prefab in spawnPrefabs)
                if (prefab.tag == "prefabOther")
                {
                    playerPrefab = prefab;
                    break;
                }
        }

        var player = Instantiate(playerPrefab);

        NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
        DebugConsole.LogWarning("User added on server(" + prefabChoice + "):" + player.tag);
    }

And I also override the OnClientConnect:

public override void OnClientConnect(NetworkConnection conn)
    {
        DebugConsole.LogWarning("User added on client:" + playerPrefab.tag);
        Message msg = new Message() { Data = "prefabA" };
        ClientScene.AddPlayer(conn,0,msg);    
    }

But the method OnServerAddPlayer(…,…,MESSAGE) is never called!!

Thanks!

Hi, there! Welcome to the forums!

It’s rare a thread goes a whole day here without being responded to, and I think the reason for that is that this isn’t really a “Getting Started” type question. Networking is definitely a more advanced subject, and I personally am not qualified to answer this for you.

I’m going to recommend this thread to the mods to move into the Multiplayer Networking subforum, where I think you’re more likely to encounter someone who knows how to help you.

Good luck!