Search Unity

Unet syncvar doesnt work from client to server

Discussion in 'Connected Games' started by PeppeDK, Jan 22, 2017.

  1. PeppeDK

    PeppeDK

    Joined:
    Aug 16, 2016
    Posts:
    3
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class Netonoffswitch : NetworkBehaviour {
    6.     [SyncVar] public bool SwitchedOn = false;
    7.  
    8.     public void CmdSwitchOnOff(bool SwitchedOn2){
    9.         Test (SwitchedOn2);
    10.     }
    11.  
    12.     [Command]
    13.     private void CmdTest(bool swichedon2){
    14.         SwitchedOn = swichedon2;
    15.     }
    16.  
    17.     [ClientCallback]
    18.     private void Test(bool swichedon2){
    19.         CmdTest (swichedon2);
    20.     }
    21.  
    22. }
    23.  
    and the code of localplayer is:
    Code (CSharp):
    1. void Update(){
    2.     if(Input.GetKeyUp(KeyCode.Mouse1)){
    3.         if (hit.collider.name == "on_off_switch") {
    4.             hit.collider.gameObject.GetComponent<Netonoffswitch>().CmdSwitchOnOff((hit.collider.gameObject.GetComponent<Netonoffswitch>().SwitchedOn ? false : true));
    5.         }
    6.     }
    7. }

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

    Zullar

    Joined:
    May 21, 2013
    Posts:
    615
    SyncVars only work Server --> Client, never from Client --> Server
     
  3. Xuzon

    Xuzon

    Joined:
    Mar 21, 2014
    Posts:
    83
    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
     
  4. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    615
    There are 2 ways to send information from Client --> Server
    1: [Command]
    2: UNET MessageBase
     
    josewebdev and Xuzon like this.