- Home /
Is there a way to create a shared variable between all the clients over network?
I am developing a racing game project in which i want to do something as simple as when any one of the client hit the finishing point display a message on all other clients showing the name of that specific player say 'John; This is the message that I want to display on all the clients connected when John hits finishing point. 'John has won' After that there is a driving script attached to all the clients which i want to disable. I've been trying to do this for 2 days now and can't seem to handle this scenario. I came to know that client can only update it's instance on other clients and cannot change their's. This is what I've done so far
[SyncVar] string winner = "";
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag.ToLower().Equals("finish"))
{
winner = pname;
CmdServerNotify(winner);
}
}
[Command] public void CmdServerNotify(string name)
{
winner = name;
}
[ClientRpc] public void RpcUpdateWinner(string name)
{
winner = name;
}
void Update()
{
if (winner == "")
{
GetComponentInChildren<TextMesh>().text = pname;
if (isLocalPlayer)
{
Camera.main.transform.position = cameraplace.transform.position;
Camera.main.transform.rotation = cameraplace.transform.rotation;
}
}
else
{
RpcUpdateWinner(winner);
GetComponent<drive>().enabled = false;
GetComponentInChildren<TextMesh>().text = winner + " has won.";
}
}
This just works for own instance of 'John' on other clients but not their's What is the most appropriate way to do this? Any help will be appreciated
Answer by JedBeryll · Nov 23, 2017 at 05:49 AM
I see you already know about SyncVar, you could make it work with that or maybe look into this: https://docs.unity3d.com/ScriptReference/Networking.SyncEventAttribute.html
How can I make it work with SyncVar? Let's say I have 2 clients a,b and a SyncVar winner now both a and b will have two copies of variable winner 4 in total i want to update all four of them i.e. 2 on client a and 2 on client b. I hope you understand what i am trying to achieve. Also I haven't looked up to SyncEvent i'll try using that too.
You lost me there mate. "two copies of variable winner" why 2 copies?
since same code is running for both a and b on client A so it creates two winner variables one for client a and other for client b and same happens on client B. Isn't this how SyncVar works? Correct me if i am wrong
Your answer
![](https://koobas.hobune.stream/wayback/20220612143212im_/https://answers.unity.com/themes/thub/images/avi.jpg)