Networking Unet how to get a var in server with the same value for all clients ?
Hi everyone.
I would like to share a var between all clients. In the case of the code below, I want "player1Score" to be in the server and the clients to act like diferent gamepads in the net. Any client should be able to call the function in the server to score up, and then the same canvas and UI Text should be appear updated in all clients. At this moment, Unity creates a version of this variable for each client, they act like local vars to the client, I want like a global var to the net.
using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI;
public class JudgeControl : NetworkBehaviour { [SyncVar] public int player1Score = 0;
[Command]
public void CmdScoreUP(int amount)
{
player1Score += amount;
print (player1Score);
}
void Update()
{
if (!isLocalPlayer)
return;
var x = Input.GetAxis("Horizontal")*0.1f;
var z = Input.GetAxis("Vertical")*0.1f;
transform.Translate(x, 0, z);
if (Input.GetKeyDown(KeyCode.Space))
{
CmdScoreUP (3);
}
}
}
Your answer
Follow this Question
Related Questions
WebGl taking too long to load. size is large 1 Answer
Server in Visual Studio and Client in Unity ?,server Visual Studio and Client unity3D ? 0 Answers
Warning: not all old messages was acknowledged 1 Answer
Basic unity networking question 1 Answer
UNET - How to wait for NetworkConnection.Send(...) to finish? 0 Answers