Send GameObject active in network to all clients

Hello,
currently I’m trying to send the state of the client/server gameobject to all people on the same server :stuck_out_tongue:

The other players on the server can’t see if I switch my PlayerMode. So you start in Hunter mode and when I switch to Hunted the others can’t see that. They see my still as Hunter :frowning:

This is my code:

var PlayerMode : int = 0; // 0 = Hunter, 1 = Hunted

var Hunter : GameObject;
var Hunted : GameObject;

function Start () {

}

function Update () {

var viewID : NetworkViewID= Network.AllocateViewID();

networkView.RPC("SetPlayerMode", RPCMode.AllBuffered, viewID, PlayerMode);

if(networkView.isMine && Input.GetKey(KeyCode.Alpha2)){
PlayerMode = 0;
}

if(networkView.isMine && Input.GetKey(KeyCode.Alpha1)){
PlayerMode = 1;
}

}

@RPC
function SetPlayerMode (viewID : NetworkViewID, Mode : int) {
 var nView : NetworkView;
   
if(PlayerMode == 0){
Hunted.gameObject.active = false;
Hunter.gameObject.active = true;
nView = Hunter.GameObject();
nView.viewID = viewID;
}
if(PlayerMode == 1){
Hunted.gameObject.active = true;
Hunter.gameObject.active = false;
nView = Hunted.GameObject();
 nView.viewID = viewID;
}
    }

I miss something and I don’t know how to get this work/go on to get this working.
Thank you. Hope you guys can help me :smiley:

Screenshot

Ok so there are a two things wrong with your code for networking:

  • You are sending an RPC every Update, that’s not likely to work, you should expect to send less frequently and interpolate for positions etc. Surely in this case you just need to send it when it changes?

  • The reason why your code isn’t working at all is that you never set the PlayerMode in the RPC call, so none of the clients actually know it!

     @RPC
     function SetPlayerMode (viewID : NetworkViewID, Mode : int) {
     var nView : NetworkView;
     PlayerMode = Mode;
    
     if(PlayerMode == 0){
        Hunted.gameObject.active = false;
        Hunter.gameObject.active = true;
        nView = Hunter.GameObject();
        nView.viewID = viewID;
     }
     if(PlayerMode == 1){
        Hunted.gameObject.active = true;
        Hunter.gameObject.active = false;
        nView = Hunted.GameObject();
        nView.viewID = viewID;
      }
    

    }

hey is there a way to do this in c#