- Home /
Send Variable.
Hello, I'm spanish, sorry for my bad English.
What I have to do in my game: I'm making a multiplayer game using the Networking brings Unity3D. I need the server to send a variable to the client (the number of players). And vice versa. I have read that I need RPC. But do not know how to use it to send a variable. I wish that they should put me an example if you could.
Thanks.
I know this will sounds rude, but you need to pick something orders of magnitude simpler to start with. Networked games are a can of worms like you wouldn't believe.
Answer by DuxDucis · Dec 19, 2012 at 11:36 PM
Ok so lets suppose you have an integer with the number of players, Ill assume it's called playerCount.
Now, you need a MainCommunicator which will hold all the starting server and server waiting room code where you will do this, assign it a NetworkView component and in your script that holds playerCount add this.
//If using Javascript
(RPC)
function SetPlayerCount(pC : int){
playerCount = pC;
}
//If using C#
[RPC]
void SetPlayerCount(int pC){
playerCount = pC;
}
This is the function the server will be running on the clients, so in the server, each time playerCount changes you should run this code.
networkView.RPC("SetPlayerCount", RPCMode.Others, playerCount);
Absolutely avoid calling this if the player is not a server, which can be done by
if (Network.peerType == NetworkPeerType.Server){
networkView.RPC("SetPlayerCount", RPCMode.Others, playerCount);
}
What if the player is not a server but still needs to send a variable to all the others, then what function will be called?
Your answer
![](https://koobas.hobune.stream/wayback/20220613085653im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
How to use RPC functions to animate other players 1 Answer
CHAT Multiplayer Problem 1 Answer
RPC Call doesnt get called 2 Answers
Requesting data from server 1 Answer