Networking Unity, I need to pass a simple variable value between Client and Server.
Help, I want to create a multiplayer game in which the player ONE can choose one of the four attacks, and when he press the button, in the device of the other player (player TWO) the gameobject of the player ONE do the animation and do damage to the player TWO. (like all Pokemon games) How can I pass variables values between the two devices and then do something? I found something about the SyncVar but I didn't understand how they works and if they are what I need to do that. Please, if you can, show me some script examples that do that.
Answer by XxSimBaaxX · May 24, 2017 at 09:31 AM
[SyncVar] is a command that synchronizes the variable between the client and the server (but not with the rest of the clients), its written right before the variable itself,example: [SyncVar] int example1; that will synchronize every value change with the server. if you want to synchronize the value between two clients then you have to synchronize the value from client one to the server and then from server to client two and you can do that by the [ClientRpc] command, example:
//this will sync the variables value from client to server
[SyncVar] int example1;
void Update ()
{
RpcSyncVarWithClients (example1);
}
//this will sync var from server to all clients by calling the "SyncVarWithClientsRpc" funtion on the clients with the value of the variable "varToSync" equals to the value of "example1"
[ClientRpc]
void RpcSyncVarWithClients (int varToSync)
{
example1 = varToSync;
}
note: ClientRpc functions have to start with "Rpc" like: void RpcExample () I hope that helped you :)
Hamza Odeh
Thank you so much. I have another big problem. On the computer it works, but I have to do it for android devices. So I used the IP address 192.168.43.1 as HOST IP and the port 6321. It works only if I use an hotspot connection between the two devices and only if the HOST device is the device which offer the hotspot....I don't understand why, I want it works with my WIFI connection, and not with the hotspot...
@V$$anonymous$$ask25 I just noticed that The note at the end is incorrect, the right one is that the Rpc has to be written at the start, like: void RpcExample () and not at the end, like: void ExampleRpc () Sorry about that little mistake, everything shall work alright now :)
Your answer
Follow this Question
Related Questions
Network Multiple Cameras 4 Answers
[UNET] Client cannot call [Command]s, only the host can 1 Answer
iOS Networking 1 Answer
[UNET] Transform sycing problem,[UNET] TRANSFORMATION SYCING PROBLEMS 0 Answers
How do I properly deal with Internal_CloneSingle can only be called from the main thread 0 Answers