SyncVar String problem

Hello everyone,

I’m facing a problem with syncing a string, it seems very simple but i’m really stuck … I explain.

I want to have a string sync between all the devices connected together.
exemple : a button with an inputField, when you type something and press button the syncVar string is modified everywhere.

But at this point my problem is, it works when use this on the host, but when i use it on a simple client, only his SyncVar string is modified.

I just don’t get it, SyncVar are not supposed to be sync ? Why does it work only on the host ?

Here is the kind of code i used :

[SyncVar]
public string testString;

    void Start()
    {
        testString = string.Empty;
    }

   void OnGUI()
    {
        if (isLocalPlayer)
        {
            GUI.Box(new Rect(10, 10, 200, 50), ("String : " + testString));

        }
    }

    public void testStringChanged(string yolo)
    {
        if (isServer)
            RpcTestString(yolo);
        else
            CmdTestString(yolo);
    }

    [ClientRpc]
    void RpcTestString(string yolo)
    {
       testString = yolo;
    }

    [Command]
    void CmdTestString(string yolo)
    {
        testString = yolo;
        RpcAudioPathp(testString);
    }

I am a beginner in networking stuff, if someone can explain the problem, or maybe the right way to do it, it’ll help me a lot.

Thanks :slight_smile:

In the default networking setup, only the server has the authority to set a syncvar. If you want a client to change a syncvar, the client has to call a [Command] on the server asking it to change the syncvar. Also, with a syncvar, you probably do not need or want an ClientRpc also changing it.

If you want a client to set a SyncVar then you must use [Command] to set the SyncVar. If you need the value to change on the client immediately then you can set the SyncVar locally on the client, but may want to use a SyncVar hook to prevent the server from being able to set the SyncVar on the client that has authority.

Thanks a lot for your answers !
After a lot of tests, i think i figured out my real problem.
Maybe i’m stupid but i thought that the syncvars was syncing on every gameobject where it is declared.
So the truth is, they’re sync on every instance of the gameobject they belong, right ?

Yes SyncVars are instance based specific to the NetworkBehaviour script they are a part of, not static/global.

I figured out a lot of things with your answers, Tanks a lot :slight_smile: