Unity multiplayer2d differents prefabs for clients

Hello , I’m new in development on unity and I have a project with a friend : to make a multiplayer game in 2D. But I have a problem. For the connection on my game I use a network manager so I create a new class who inherits Network Manager. I use a override method OnServerAddPlayer:

public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
if (choix == 1)
{
GameObject player = (GameObject)GameObject.Instantiate(perso1, Vector3.zero, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
NetworkServer.Spawn(player);
}
if (choix == 2)
{
GameObject player = (GameObject)GameObject.Instantiate(perso2, Vector3.zero, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
NetworkServer.Spawn(player);
}
}
And i use too OnclientConnect :
public override void OnClientConnect(NetworkConnection conn)
{
ClientScene.Ready(conn);
ClientScene.AddPlayer(0);
}

I want that a client choose an prefab for be connected in the game but when I change my attribut “choix” the prefab of my client do not change. Please can you help me ?

Up

Please use Code Tags when posting code snippets so it’s easy for people to read your code and give advice:

Sorry i m new here.
So my code it’s :

public class ScriptNetworkManager : NetworkManager {

    public GameObject perso1,perso2;
    public int choix = 1;
    static  GameObject selection;
    // Use this for initialization
    void Start () {}
  
    // Update is called once per frame
    void Update () {
    }
    void OnGUI()
    {
        if (GUI.Button(new Rect(400, 70, 100, 50), "Rouge"))
        {
            choix = 1;
        }

        if (GUI.Button(new Rect(550, 70, 100, 50), "Bleu"))
        {
            choix = 2;
        }
    }

    public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    {
        //Resources.Load("Prefebs/spriteV2-rouge_0 1")as GameObject
        if (choix == 1)
        {
            GameObject player = (GameObject)GameObject.Instantiate(perso1, Vector3.zero, Quaternion.identity);
            NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
            NetworkServer.Spawn(player);
        }
        if (choix == 2)
        {
            GameObject player = (GameObject)GameObject.Instantiate(perso2, Vector3.zero, Quaternion.identity);
            NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
            NetworkServer.Spawn(player);
        }
    }

    public override void OnClientConnect(NetworkConnection conn)
    {
        ClientScene.Ready(conn);
        ClientScene.AddPlayer(0);
    }

}

If you can help me I would be very reconnaisant.

I’m not very familiar with the networking framework in Unity, but from what I know, you might need to use the [Sync] attribute on the “choix” variable, to make it replicate to the clients.

Yes, but the problem is that it’s impossible to use Sync with the NetworkManager, He need the NetworkBehaviour but if i use this class I don’t redefine the OnserverAddPlayer.

So I do not know what to do.

Perhaps it’s possible to get the new connection, and then wait for the user to make a choice before spawning the player. So have the client connect, the client will see the choice screen, and then once they choose one (maybe with a coroutine that waits for (choix != 0), the server creates the player.

Thanks i try this method