Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Unity Multiplayer SyncVar Hooks not receiving updated values

Discussion in 'Connected Games' started by tango209, Dec 11, 2015.

  1. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    378
    The following code is attached to the player object which has a Network Identity and is marked as Local Player Authority. The host's Thirst and Hunger values are updated correctly, but on the client the values are always what was set in the prefab.

    Does anyone have an idea of what I'm doing wrong? Thanks in advance.

    Code (CSharp):
    1. public class Vitals : NetworkBehaviour
    2. {
    3.   [SyncVar(hook = "OnHealthChange")] public float Health;
    4.   [SyncVar(hook = "OnThirstChange")] public float Thirst;
    5.   [SyncVar(hook = "OnHungerChange")] public float Hunger;
    6.  
    7.   public float DefaultHealth;
    8.   public float DefaultThirst;
    9.   public float DefaultHunger;
    10.   public float ThirstRate;
    11.   public float HungerRate;
    12.  
    13.   public UIUnitFrame_Bar HealthBar;
    14.   public UIUnitFrame_Bar ThirstBar;
    15.   public UIUnitFrame_Bar HungerBar;
    16.  
    17.   public override void OnStartLocalPlayer()
    18.   {
    19.     this.HealthBar = GameObject.Find("Health Bar").GetComponent<UIUnitFrame_Bar>();
    20.     this.ThirstBar = GameObject.Find("Thirst Bar").GetComponent<UIUnitFrame_Bar>();
    21.     this.HungerBar = GameObject.Find("Hunger Bar").GetComponent<UIUnitFrame_Bar>();
    22.  
    23.     Assert.raiseExceptions = false;
    24.     Assert.IsNotNull(this.HealthBar, "HealthBar not set");
    25.     Assert.IsNotNull(this.ThirstBar, "ThirstBar not set");
    26.     Assert.IsNotNull(this.HungerBar, "HungerBar not set");
    27.  
    28.     this.HealthBar.maxValue = (int)this.DefaultHealth;
    29.     this.ThirstBar.maxValue = (int)this.DefaultThirst;
    30.     this.HungerBar.maxValue = (int)this.DefaultHunger;
    31.   }
    32.  
    33.   [Client]
    34.   public void FixedUpdate()
    35.   {
    36.     if (this.isLocalPlayer)
    37.       CmdHeartBeat(Time.deltaTime);
    38.   }
    39.  
    40.   [Command]
    41.   public void CmdHeartBeat(float elapsed)
    42.   {
    43.     this.Thirst = this.Thirst > 0f ? this.Thirst - this.ThirstRate * elapsed : 0f;
    44.     this.Hunger = this.Hunger > 0f ? this.Hunger - this.HungerRate * elapsed : 0f;
    45.   }
    46.  
    47.   [Client]
    48.   public void OnHealthChange(float value)
    49.   {
    50.     if (this.isLocalPlayer)
    51.       this.HealthBar.value = (int)this.Health;
    52.   }
    53.  
    54.   [Client]
    55.   public void OnThirstChange(float value)
    56.   {
    57.     if (this.isLocalPlayer)
    58.       this.ThirstBar.value = (int)this.Thirst;
    59.   }
    60.  
    61.   [Client]
    62.   public void OnHungerChange(float value)
    63.   {
    64.     if (this.isLocalPlayer)
    65.       this.HungerBar.value = (int)this.Hunger;
    66.   }
    67. }
    68.  
     
  2. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    643
    Why are you making your hooks Client attributes?
     
  3. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    378
    Slapped them on when I was getting frustrated, doesn't make a difference.
     
  4. Wtyson

    Wtyson

    Joined:
    Aug 15, 2014
    Posts:
    53
    [SyncVar(hook = "OnHealthChanged")]
    public float health = 100;

    void OnHealthChanged(float hlth)
    {
    health = hlth;

    }

    try doing it like this with out your if statements as long as you got a local player setup script to designate your player scripts you dont need the islocal player check
     
    tango209 likes this.
  5. ObliviousHarmony

    ObliviousHarmony

    Joined:
    Jul 3, 2014
    Posts:
    79
    Hey tango,

    When using the hooks, you have to actually set the value for the variable yourself. This allows you to discard information from the server if you so desire. For example,

    Code (csharp):
    1.  
    2. public void OnHealthChange(float value)
    3. {
    4.     Health = value;
    5.     if (this.isLocalPlayer)
    6.         this.HealthBar.value = (int)this.Health;
    7. }
    8.  
     
    tango209 and DRRosen3 like this.
  6. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    378
  7. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    643
    Yup! Don't know how I missed that. Guess I need to take a day or two off of looking at code. lol
     
    tango209 likes this.
unityunity