Best way to access player's gameobject across network

So I’m trying to make a simple player customizer in a multiplayer game.

Currently I have:

  • Clicked button sends choice to “local” player
  • Player RPC’s network manager (so network knows who wants to switch)
  • Manager RPC’s the change to everyone

I can’t figure out how to keep track of - and reference - the players correctly though.
For example, NetworkViewID doesn’t seem to be convertible to something useful
(like the gameobject it’s attached to). I was thinking of putting the players in an array
but I don’t know how to implement it on a network correctly.

What am I missing?

Any help appreciated

You need to assign a unique ID to each of your player, something like an index starting at 0 for the first player. On all clients, keep an array of all players (it must be the exact same array). Then, when the player clicks on the button, RPC the action information to all players (or all other players) by sending the playerID:

nView.RPC("ExecuteActionOnPlayer", RPCMode.OthersBuffered, playerID);

Then, when the other players receive the RPC message, they will execute the action based on the playerID you sent them:

void ExecuteActionOnPlayer(int playerID)
    Player player = playersArray[playerID];

    // Execute "player" action here

You have to make sure that the array stays the same on all clients, but it shouldn’t be too hard since players don’t usually get modified every frame. I think the correct way to implement this is to build the array once on all connected players, and if you ever need to make a change to it, synchronize it via an RPC call. You can also send more than one ID if you want to execute an action that involves more than one player.

If this is not what you wanted, please give us more explanation on exactly what you want to do over the network.

with my last multiplayer projects, I simply have the host assign the other connecting machines a simple player number in a RPC call soon as they each connect.

once you have that, everything else is simple.

when a player makes his selection, I manually include the player number somewhere in the RPC call.
this makes it easy for all the connected machines to keep track of who is doing what.

then when you get to making your Arrays of player selections, obviosly the player number would be the same as the arrays index number representing the player. so it makes it very easy to assign incomming info directly into your arrays.