Search Unity

[FIXED] Variable Synchronisation Question

Discussion in 'Multiplayer' started by Grazer2, Jul 10, 2015.

  1. Grazer2

    Grazer2

    Joined:
    Jun 20, 2011
    Posts:
    86
    Hey there,

    I'm struggling with finding an efficient way to solve a specific variable syncing issue. I'll give a concrete example:

    The server has a string variable called gameState. The server may cycle through different gameStates like Intro, Warmup, Playing, Finished. I want all connected clients to know the latest value of this variable.

    Now I'm not struggling with [SyncVars], I think I understood that concept fairly well. What I'm struggling with is how to set up classes and prefabs in a way that allows the server to set this variable at one central point of code and that allows for the clients to read it from that one central point.

    Ideally I'd like to have a gameObject with a custom script, and the server writing into a syncVar of the script, and the clients to read that syncVar from it. When setting up a prefab in that way however it seems to be non-existing on client applications. Note that I did derive the script from NetworkBehaviour and also added a NetworkIdentity component to the prefab (with ServerOnly as well as Local Player Authority turned Off). I did not however specifically spawn this prefab through the NetworkManager. I just added it to the online scene.

    As a workaround I'm currently writing this gameState variable as syncVar into all existing players. This however means that there are for example 5 players with 5 occurances of the same variable. I'd just like to have it in one central spot. Anybody got any ideas on how to solve this in a simple way?

    Thanks and cheers
     
  2. Grazer2

    Grazer2

    Joined:
    Jun 20, 2011
    Posts:
    86
    Nevermind. I figured it out. My problem was a general code issue. I didn't know my prefab would automatically become deactivated as long as the client didn't connect to the host yet.
    So the solution was to react to the fact that, when the game starts, the gameObject containing the sync script may be on a gameObject that's deactivated (automatically by the UNet system), and that becomes activated once connected to a network game.