Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Help Wanted Issue with NetworkStart and maybe NetworkManager.Singleton.ConnectedClientsList.Count

Discussion in 'Netcode for GameObjects' started by Erenquin, Aug 25, 2021.

  1. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    135
    Hello,
    I'm doing a small game.
    With 2 players, it was mostly working, but when I try to upgrade to 4 players I get some issues, currently for the scoring system.

    In the Connection manager I use the "ConnectedClientsList.Count", as index in a list of spwan points and it works perfectly well. The 4 players are spawned at the correct positions:
    Code (CSharp):
    1.         int ClientIndex = NetworkManager.Singleton.ConnectedClientsList.Count - 1;
    2.         callback(true, null, approve, ClientSpawnTransf[ClientIndex].position, ClientSpawnTransf[ClientIndex].rotation);
    But for the scoring I'm trying something similar, and there the "ConnectedClientsList.Count" is zero, I tried both in "NetworkStart" and "Start".

    Actually the ClientIndex variable is correct on the Host, but not on the clients and is defined as a NetworkVariable
    Code (CSharp):
    1.    NetworkVariableInt ClientIndex = new NetworkVariableInt(-1);

    Code (CSharp):
    1.     public override void NetworkStart()
    2.     {
    3.         if (IsLocalPlayer)
    4.         {
    5.             base.NetworkStart();
    6.             //Debug.Log("Connect clients "  + NetworkManager.Singleton.ConnectedClientsList.Count);
    7.             ClientIndex.Value = NetworkManager.Singleton.ConnectedClientsList.Count - 1;
    8.         }
    9.     }
    10.  
    11.     void Start()
    12.     {
    13.         Debug.Log("Connect clients " + NetworkManager.Singleton.ConnectedClientsList.Count);
    14.     }
    15.  
     
  2. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    135
    Should I use "if (IsClient) " instead of "IsLocalPlayer" ?

    In the score update method I use "IsClient".
     
  3. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    248
    The ConnectedClientsList is only propagated on the server/host and will always been empty on clients. If you need this value you can use a NetworkVariable to synchronize it from the server to the clients.
     
  4. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    135
    Thanks a lot for the answer, ultimately that is what I did and it works fine:
    Code (CSharp):
    1.     public override void NetworkStart()
    2.     {
    3.         if (IsLocalPlayer)
    4.         {
    5.             ClientConnectionServerRpc();
    6.         }
    7.     }
    8.  
    9.     [ServerRpc]
    10.     void ClientConnectionServerRpc()
    11.     {
    12.         ClientIndex.Value = NetworkManager.Singleton.ConnectedClientsList.Count;
    13.     }
     
unityunity