- Home /
Change variable on all players with UNet
Hello :)
I have two players, one of them is host and second one want to change variables on all players.
How can I change var on all players from client? I need that because I'am developing simple pause network system. Anyone can help me? Thanks a lot! :)
Answer by RkSanders · Jul 12, 2015 at 04:11 PM
You are meant to use the SyncVar attribute.
[SyncVar]
bool pause;
It will automatically keep clients updated with the value of the variable on the server. Additionally you can add a hook function if you want the client to know when the value changes:
[SyncVar(hook = "OnPause")]
bool pause;
void OnPause(bool newValue)
{
print("Pause is now: " + newValue);
}
Yeah, but it only changes var on P2 and I need change var on all players (on server and client). If you want to build pause system, you need to pause all players on network. But thanks for response. :)
SyncVars are sent to all clients and are buffered. You just need to check the value of the Syncvar on the hosts object.
But if I change var on localPlayer and call Command to change var on server, P1 has still paused on false and I need pause everything, everywhere.. What about option when I create another game object, which ad$$anonymous$$isters players. So if there is a change of variable paused, than all players are paused. And variable paused is SyncVar.
The client shouldn't set the pause bool. I would recommend: the client asks host to pause with a Command method, host sets pause bool and syncvar updates the host's object on all clients and OnPause() hook is called, OnPause() hook sets game to paused.
If you need special logic on every client object, yes I would create a manager class that handles the pause with one method call i.e. from the OnPause() hook.
Okay, so I implement Command method to Player script,which sets up bool var on Pause$$anonymous$$anager on all players, but how I can deter$$anonymous$$e that localPlayer is host?
Your answer
Follow this Question
Related Questions
[UNet] How I can send Command to non-player object? 1 Answer
How to synchronize pause and inventory scripts 2 Answers
UNET Server not setting [SyncVar] 0 Answers
Networking Sync SetActive Not Working 0 Answers
Multiple Cars not working 1 Answer