Search Unity

Customize each player color not behaves well

Discussion in 'Multiplayer' started by aralambomahay, Apr 21, 2017.

  1. aralambomahay

    aralambomahay

    Joined:
    Apr 12, 2017
    Posts:
    10
    Hi,

    I am creating a little multiplayer game in which each player can choose his own prefab and colour. I succeed to instantiate each player prefab but about the colours, I only see the colour change only in the server scene.

    I attached the screen shot of my issue below. The issue is that every player updates only his colours not others.
    Here is the code where I change the colour:

    Code (CSharp):
    1. public override void OnStartLocalPlayer()
    2.     {
    3.         base.OnStartLocalPlayer();
    4.         m_localColor = UserInformation.PlayerColor;
    5.         CmdChangePlayerColorInServer(m_localColor);
    6.     }
    7. [Command]
    8.     public void CmdChangePlayerColorInServer(Color c)
    9.     {
    10.              GetComponent<MeshRenderer>().material.color = c;
    11.             RpcUpdateClientsColor(c);
    12.  
    13.     }
    14. [ClientRpc]
    15.     public void RpcUpdateClientsColor(Color c)
    16.     {
    17.             GetComponent<MeshRenderer>().material.color = c;
    18.  
    19.     }
    I also try to send a RPC call from the server to every player but colours update don't occur for other player.

    Thanks!
     

    Attached Files:

  2. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    Commands and ClientRPC's don't always work. I think Commands can get dropped if things aren't initialized and it doesn't recognize authority yet. And RPC's get can get dropped if things aren't initialized and it doesn't recognize the clients as observers. So you problem *may* lie in there. Try adding in a 1sec delay before CmdChangePlayerColorInServer is called see if it fixes things. Or try and see where the breakdown occurs (is it during the Command or during the RPC).

    But on a bigger pictures RPC and Command is not the proper way to handle this. The history of past Commands/RPC's are not buffered so when a new player connects the so they will not know the color of other existing players. You must either use SyncVars or re-send the RPC to the newly connected client for him to get all the latest colors (again after things are initialized and the new client is an observer).

    Gets complicated pretty quick doesn't it :p
     
  3. aralambomahay

    aralambomahay

    Joined:
    Apr 12, 2017
    Posts:
    10
    The simple way I saw is to re-send the RPC to the newly connected client like you say and it's working but I don't know if it is the better way because i have to loop each player in the server and then do the RPC call to its PlayerController script attached.

    I also tried to use SyncVar but I didn't succeed.