- Home /
UNET : SyncVar value not updating, hooks firing
So I've run into a strange issue where SyncVar values are not updating on my clients, however their hook functions are being called!
In the game I am currently making I only track collisions and their related events on the server itself. So I have a damage Chain that Looks something like this.
First when a "bullet" object hits a player this code is run on the server instance of the bullet
protected virtual void OnTriggerEnter2D (Collider2D collider) {
if (!isServer || collider.gameObject == shooter.gameObject)
return;
Player playerHit = collider.GetComponent<Player>();
if (playerHit)
playerHit.health.ServerRecieveDamage(damage);
NetworkServer.Destroy(gameObject);
}
Then damage is applied on the players health component. After damage is applied it fires off some client events based on the damage taken.
[SyncVar(hook="CallbackTakeDamage")]public int health;
[Server] public virtual void ServerRecieveDamage (int damageAmount) {
health -= damageAmount;
Debug.Log(health.ToString() + " is how much health we have!");
}
[ClientCallback] protected virtual void CallbackTakeDamage (int newHealth) {
Debug.Log(health.ToString());
if (newHealth <= 0) {
if (DeathEvent != null) DeathEvent();
} else {
if (TakeDamageEvent != null) TakeDamageEvent();
}
}
Everything appears to get called fine. However the sync var doesn't appear to update it's value on any of the clients! However the callback function is invoked everywhere (they just stay at max health). I haven't run into anything like this before. Normally it's an all or nothing thing. I have never seen partial functionality from sync vars like this. Am I doing something wrong here?
Answer by Munchy2007 · Jan 02, 2016 at 12:27 PM
you need to do health = newHealth;
in your hook function.
[ClientCallback] protected virtual void CallbackTakeDamage (int newHealth) {
health = newHealth;
Debug.Log(health.ToString());
10.
if (newHealth <= 0) {
if (DeathEvent != null) DeathEvent();
} else {
if (TakeDamageEvent != null) TakeDamageEvent();
15. }
}
When you use hook functions the syncvar isn't automatically updated, this is by design.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
How to call [Command] on Client in UNet 1 Answer
Animation & sound over network 1 Answer
Syncing terrain over Network 0 Answers
UNET AddPlayerForConnection with already existing player GameObject 0 Answers