Why is the list not updated on the remote?

I have the following code on the players:

    private GameObject button1;
    private GameObject button2;
    public List<string> localCopyOfList = new List<string> ();

    [Command]
    void Cmd_AddToList() {

        Rpc_AddToList ();

    }

    [ClientRpc]
    void Rpc_AddToList() {
        print ("Rpc_AddToList");

        // this code is executed on all clients
        localCopyOfList.Add("#1");
        print (">> " + localCopyOfList [0]);
        localCopyOfList.Add("#2");
        localCopyOfList.Add("#3");
        localCopyOfList.Add("#4");
        localCopyOfList.Add("#5");
    }

    void PrintTheList() {
        print ("PrintTheList: " + localCopyOfList.Count);
        foreach (string mySTR in localCopyOfList) {
            print ("> " + mySTR);
        }
    }

Button1 = Add to List
Button2 = PrintTheList

However, localCopyOfList is empty on the remote player and I do not understand why. The print in the Rpc shows the data in the list but when doing the PrintTheList the List is empty.

I tried your script and it seems to partially works :
If the Host press Button 1, both client can press Button 2 and the print is done on the client that press it.
If the Remote client press Button 1, nothing happens if the script is not on the remote player object due to authority issues.

This is a kind of limitation from Unity Unet which works with the idea that any Remote Client can only directly controls one game object (and its children) remotely in a scene hosted on another client while the hosting client may controls any other on top of sending calls onto the remotes ones.

It’s a bit like remote player were a foot soldier while the host controls the rest of the army. The only way the “foot soldier” can act onto other soldiers is through the chain of command so he need to request his superior (the server) to send the order to other troops.

Now, the way you can do this so that the remote player can send the order from the [command] function is by passing any call through a script that is placed onto the remote player’s gameobject. (It can be the parent which has the Network Identity or any of its children.)

So, to explain it in a simple way :
• Each Remote Player can only communicate to the server through 1 Game Object with 1 single Network Identity. (This includes any childs of that Game Object.)
• All remaining Game Object with a Network Identity are managed by the server automatically and no remote player have any authority on those.
• To manage buttons (UI) to act onto the server, you got to pass any call through the 1 single Game Object with 1 single Network Identity that is recognized with authority from the server.

In other words, you should make sure that the player “game object” that is spawned when a client join a hosting client to have some kind of “radio” script that can communicate whatever you need each remote client to communicate and a receiver on the server’s end that can translate the message toward the right destination.

Who said that building a online game wasn’t like fighting a war? :wink:

1 Like

That was a great answer, thank you very much :slight_smile:

Happy to help. Actually, I’m also kinda new to Unet so answering your question and testing it out helped me out understanding more of it.

You actually saved me quite some headache and error on my end by making me looking it up. :stuck_out_tongue: