Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

RPC and Command Calls OR Network messages for syncing variables across Server and Client?

Discussion in 'Connected Games' started by jnguyen54228, Nov 7, 2018.

  1. jnguyen54228

    jnguyen54228

    Joined:
    Mar 2, 2018
    Posts:
    2
    I'm currently making a Unity Multiplayer game that relies on the synchronization of variables across the Server and the singular Client. I'm already able to use [SyncVar] to sync variables from the server with the client, but not the other way around. The thing is, I don't have a player prefab that spawns in the game. Am I still able to make Command and RPC calls? I tried this code below which didn't end up working when I played the game. Would Network messages work better for my situation?

    bool officeBought = false;

    void Update () {
    if (!isServer)
    {
    CmdSendBuildingBought(officeBought);
    }
    }

    [Command]
    public void CmdSendBuildingBought(bool buildingBought)
    {

    buildingBought = true;
    }
     
  2. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    2,665
    What do you mean with 'you don't have a player'?
    Where does the server send the data to then?
    Commands and RPCs definitely need a NetworkIdentity on a GameObject.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    3,812
    Why would you use the HLAPI without a player object? It doesn't need to be have a model in the game. You can just use it as a convenient place to store all the Commands you want to send back to the server. Then you just sync variables using SyncVars from the server to the clients, and if you need to update one from a client you send it to the server using a Command on the Player object.