Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unet syncvar doesnt work from client to server

Discussion in 'UNet' started by PeppeDK, Jan 22, 2017.

  1. PeppeDK

    PeppeDK

    Joined:
    Aug 16, 2016
    Posts:
    8
    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:
    650
    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:
    650
    There are 2 ways to send information from Client --> Server
    1: [Command]
    2: UNET MessageBase
     
    josewebdev and Xuzon like this.