- Home /
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 :P
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 :(
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 :D
So what exactly is your problem? Does the RPC not get called? Or is it the state synchronization which doesn't work?
The other players on the server can't see if I switch my Player$$anonymous$$ode. So you start in Hunter mode and when I switch to Hunted the others can't see that. They see my still as Hunter :(
Hmm. Cool it works, but only when someone will connect spawn and not when they are on the server :(
Answer by whydoidoit · May 18, 2013 at 08:40 PM
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; } }
It's down to the order of your calling in Update as I mentioned - it should be like this:
function Update () {
var viewID : NetworkViewID= Network.AllocateViewID();
if(networkView.is$$anonymous$$ine && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Alpha2)){
networkView.RPC("SetPlayer$$anonymous$$ode", RPC$$anonymous$$ode.AllBuffered, viewID, 0);
}
if(networkView.is$$anonymous$$ine && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Alpha1)){
networkView.RPC("SetPlayer$$anonymous$$ode", RPC$$anonymous$$ode.AllBuffered, viewID, 1);
}
}
Thank ! I already did that ;) Cool its working. Sorry.... It's late here and I'm tired ... (headbang) yes sure it will not work without Player$$anonymous$$ode = $$anonymous$$ode :S Thanks :D
Hmm, i don't think it's wise to call Network.AllocateViewID every Update. Each allocated ViewID has to be unique on all clients. I haven't tested it yet, but i guess Unity will send this information to all clients.
Answer by Black_Hole_Games · Aug 28, 2017 at 12:25 PM
hey is there a way to do this in c#
It's the same code. You only need to convert it to C#. But it's for the legacy networking system. You can still use it, but you shouldn't. Here you can find a tutorial series for the new networking system (Unet): https://unity3d.com/learn/tutorials/topics/multiplayer-networking
However, you can also translate it easily to Photon Networking API.
As it seems you don't know how to translate the JavaScript code into C# I guess you're a beginner. Check https://unity3d.com/learn/tutorials to get into program$$anonymous$$g a little better ;)
Well I will translate it for you, but keep in $$anonymous$$d it would be better if you could it yourself and you need to import the correct namespaces otherwise it won't work:
@RPC
void SetPlayer$$anonymous$$ode (NetworkViewID viewID, int $$anonymous$$ode) {
NetworkView nView;
Player$$anonymous$$ode = $$anonymous$$ode;
if(Player$$anonymous$$ode == 0){
Hunted.gameObject.SetActive(false);
Hunter.gameObject.SetActive(true);
nView = Hunter.GameObject();
nView.viewID = viewID;
}
if(Player$$anonymous$$ode == 1){
Hunted.gameObject.SetActive(true);
Hunter.gameObject.SetActive(false);
nView = Hunted.GameObject();
nView.viewID = viewID;
}
}
void Update () {
NetworkViewID viewID = Network.AllocateViewID();
if(networkView.is$$anonymous$$ine && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Alpha2)){
networkView.RPC("SetPlayer$$anonymous$$ode", RPC$$anonymous$$ode.AllBuffered, viewID, 0);
}
if(networkView.is$$anonymous$$ine && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Alpha1)){
networkView.RPC("SetPlayer$$anonymous$$ode", RPC$$anonymous$$ode.AllBuffered, viewID, 1);
}
}
Your answer
Follow this Question
Related Questions
Problem in Instantiate Gameobject Through Network 0 Answers
Network RPC find sender gameobject 2 Answers
How to get a gameObject having viewID? 1 Answer
Active/Dis-active Gameobject By RPC 1 Answer
Deleteing Objects created by Clients 0 Answers