Search Unity

SyncVars in GameManager Not Working

Discussion in 'Multiplayer' started by Deleted User, May 28, 2016.

  1. Deleted User

    Deleted User

    Guest

    In my game, I have a lot of singletons that more or less run the game's scene to scene state and logic. To use Host Migration, some of the variables in these singletons will need to be in SyncVars. How does this work for objects that aren't spawned?

    I read about scene objects in the manual, but that doesn't seem to work right in this case. I have a NetworkIdentity on my GameManager object that's created in my first scene and has DontDestroyOnLoad() in its Awake(). When running a multiplayer game, the client doesn't get the updated values for variables that are SyncVars.

    Does anyone have a suggestion?
     
  2. Lexeon

    Lexeon

    Joined:
    Apr 22, 2016
    Posts:
    23
    One thing to note is that SyncVar variables with hooks are set to the default value for any client that logs in. So, you would need to have the class extend the NetworkBehaviour class, and override the OnStartClient method. For example:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class Health : NetworkBehaviour {
    6.  
    7.     public const int maxHealth = 100;
    8.  
    9.     [SyncVar(hook = "OnHealthDecreased")]
    10.     public int currentHealth = maxHealth;
    11.  
    12.     void OnHealthDecreased(int health) {
    13.         //This next line is to update health for clients
    14.         currentHealth = health;
    15.     }
    16.  
    17.     public override void OnStartClient() {
    18.         OnHealthDecreased(currentHealth);
    19.     }
    20. }
    21.  
    Also, what are your SyncVar variables? As stated in the manual, only certains primitives and objects can have the SyncVar attribute: http://docs.unity3d.com/Manual/UNetStateSync.html
     
  3. Deleted User

    Deleted User

    Guest

    Thank you. All good points. The SyncVars are floats without hook functions. They're updated on the server and should propagate out to the client. I run the server in a standalone build and the client in the editor so I can watch the variables in the inspector. The variables don't change at any point while watching them in the inspector. Could my test be flawed?
     
  4. Oshroth

    Oshroth

    Joined:
    Apr 28, 2014
    Posts:
    99
    Try increasing your loglevel on NetworkManager to see if the server is sending the SyncVars and if the clients are receiving them.
     
  5. Deleted User

    Deleted User

    Guest

    The SyncVars in my GameManager are being sent by the host but are not received by the client. Other SyncVars, ones not in scene objects, are being sent and received correctly. Could there be some undocumented requirement for scene objects in order for them to work correctly, or perhaps this is a bug?
     
  6. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    @rikey did you find a solution for this?
    I can't see figure out how to deal with NetworkBehaviours already existing in the scene as opposed to spawned ones.