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.
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:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class Health : NetworkBehaviour {
public const int maxHealth = 100;
[SyncVar(hook = "OnHealthDecreased")]
public int currentHealth = maxHealth;
void OnHealthDecreased(int health) {
//This next line is to update health for clients
currentHealth = health;
}
public override void OnStartClient() {
OnHealthDecreased(currentHealth);
}
}
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?
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?
@ 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.