Unet syncvar doesnt work from client to server

Hi all,
i have a cube that is interactive, so when you press right button on it, a syncvar bool will be true if false or false if was true

so the code of cube is:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class Netonoffswitch : NetworkBehaviour {
    [SyncVar] public bool SwitchedOn = false;

    public void CmdSwitchOnOff(bool SwitchedOn2){
        Test (SwitchedOn2);
    }

    [Command]
    private void CmdTest(bool swichedon2){
        SwitchedOn = swichedon2;
    }

    [ClientCallback]
    private void Test(bool swichedon2){
        CmdTest (swichedon2);
    }

}

and the code of localplayer is:

void Update(){
    if(Input.GetKeyUp(KeyCode.Mouse1)){
        if (hit.collider.name == "on_off_switch") {
            hit.collider.gameObject.GetComponent<Netonoffswitch>().CmdSwitchOnOff((hit.collider.gameObject.GetComponent<Netonoffswitch>().SwitchedOn ? false : true));
        }
    }
}

it work fine from server to client, but it doesnt work from client to server. any help is appreciated.

SyncVars only work Server → Client, never from Client → Server

As @Zullar says it doesn’t work like that to sync things from client to server, to do this you will need to use a [Command] from the client object to the server

There are 2 ways to send information from Client → Server
1: [Command]
2: UNET MessageBase

2 Likes