Updating variables

I am working on a project where there are 2 players, 1 is a host/client, and the other is a client and I want both of the players to be able to have access to and the ability to update the variables of a game object’s script and have that change be updated on both the server and the client, regardless of who changes it.

Scenario 1
Player1(Server/client) changes int numOfUnits from 10 to 9.
Player1(Server/client) now sees int numOfUnits as 9.
Player2(Client) now see’s int numOfUnits as 9.

Scenario 2
Player2(Client) changes int numOfUnits from 9 to 8;
Player 2(Client) now sees int numOfUnits as 8.
Player1(Server/Client) now sees int numOfUnits as 8.

If I’m reading the unity documentation correctly, SyncVars functionality doesn’t seem to be the right approach, because variables are only updated and broadcast to clients if the server itself makes changes to the game object script’s variables and then broadcasts the change to clients, but not the other way around (correct me if I’m wrong there).

What methodology would you suggest as a solution to allow a sort of 2 way street, where 1 game object can be modified by both the server and the client and both the server and the client see’s the changes?

I’m very new to unity networking. Sorry if this is a question that’s been asked before.

To send information from Client → Server there are 2 UNET tools
1: [Commands]
2: MessageBase

Commands are only able to be used if the client “owns” that object… like your player. But it sends it directly to that NetworkBehaviour script on the server which is nice.

MessageBase allows you to send any data at any time. They can be used to send from Client → Server and from Server → Client(s). This is the most flexible if commands will not work for you.

Thank you for your reply! This helps a lot. The issue is that basically I want something like a script containing variables to be updated by both the Host/Client player and the 2nd player who is just a client.

So sounds like what you’re saying is that Commands probably won’t work in this situation? I’m going to read through the MessageBase documentation and see if I can get something working that way.