5
\$\begingroup\$

I'm trying to Sync data from the client to the server using the new Unity Networking, and I'm failing for some reason.

I've set up a prefab with a NetworkIdentity component with the attribute Local Player Authority set.

I (manually) spawn that on the server, and it is correctly propagated to the clients.

I have a couple of vars on this class, which the client sets and the server should read:

public class PlayerController : NetworkBehaviour
{
    [SyncVar] public bool engineOn;
    [SyncVar] public Quaternion lookAt;

The client correctly sets them, however the server never receives them, they always stay at the default value.

What might be happening?

\$\endgroup\$
1
  • \$\begingroup\$ The alternative is using Command, but I would like to understand if I can get it to work using SyncVar. \$\endgroup\$
    – o0'.
    Jun 15 '15 at 16:56
4
\$\begingroup\$

[SyncVar] only sends messages from the server to the client. You need [Command].

\$\endgroup\$
3
  • \$\begingroup\$ You confirm this is also true even for objects with Local Player Authority? \$\endgroup\$
    – o0'.
    Jul 8 '15 at 10:28
  • \$\begingroup\$ PS: why the downvote? \$\endgroup\$
    – o0'.
    Jul 8 '15 at 10:28
  • 1
    \$\begingroup\$ Yes, I tested it on my cumputer and checked online document even if with Local Player Authority \$\endgroup\$
    – Ricardo
    Jul 12 '15 at 17:18
1
\$\begingroup\$

Not sure if this has been figured out or but here goes. I dropped the [SyncVar] portion except for health and shields on players and used the following code to get by break lights to sync across network.

void LateUpdate () {

    if (isLocalPlayer) {
        if (Input.GetButton ("Fire1")) {
            breakLightsOnOff = true;
        } else {
            breakLightsOnOff = false;
        }
        CmdBLC (breakLightsOnOff);
    }
}

[Command]
void CmdBLC(bool OnOff) {
    RpcBreakLightControl (OnOff);
}

[ClientRpc]
void RpcBreakLightControl(bool OnOff) {
    breakLights.SetActive (OnOff);
}
\$\endgroup\$
1
  • \$\begingroup\$ This was a huge help for me as I was using Command methods and SyncVars but hadn't used Rpc methods \$\endgroup\$
    – mswieboda
    Mar 1 '17 at 3:48
-1
\$\begingroup\$

The problem is that Server will never receive these updates unless there is a LocalClient on the machine running the server. Refer unity docs for the word Host i.e. Sever and a Client

\$\endgroup\$
2
  • \$\begingroup\$ So, local authority has no effect on syncvars? That would be very counter-intuitive… \$\endgroup\$
    – o0'.
    Jun 21 '15 at 8:16
  • \$\begingroup\$ The intension was to be able to turn any Single Player Game to Multiplayer in a few hours. If there is a client on the Server as well, one does not have to write special code for remote client and local client. It may be Counter-Intuitive but it is Productive. This way games can have dedicated servers. \$\endgroup\$ Jun 21 '15 at 17:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .