- Home /
Passing a variable from a client to server
Basically, all I want to know is how can I get a client to send a variable to the server. Here is an example of what I tried in javascript:
var MyName : String = "Example Name";//Your username
var Player1Name : String = "";//How the server will store your username
function OnConnectedToServer(){
SendUserNameToServer();
}
@RPC
function SendUserNameToServer(){
if(Player1Name == "")
{
Player1Name = MyName;
}
}
Yes, I have attached a network view component to the gameobject holding this script. How can I get this to work properly? Thanks.
Answer by mediumal · Aug 11, 2014 at 01:38 PM
A few things wrong here.
Since SendUserNameToServer() has the @RPC attribute it is called by Unity when the RPC is received. To actually send the RPC you need to use the RPC() method on the NetworkView component.
You will also need to add a name parameter for the incoming name. Here is an example:
function OnConnectedToServer(){
SendUserNameToServer();
}
function SendUserNameToServer(){
networkView.RPC("SetPlayer1Name", RPCMode.Server, MyName);
}
@RPC
function SetPlayer1Name(name){
if(Player1Name == "")
{
Player1Name = name;
}
}
Your answer
Follow this Question
Related Questions
Sending an RPC 1 Answer
Network Chat not sending messages to other clients 1 Answer
Where is a free, helpful tutorial on how to use Unity's networking? 2 Answers
RPC failed to send 1 Answer
A couple of questions about Networking. 0 Answers