UNET Documentation

Missed assigning of the SyncVar value in the hook

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;

public class Health : NetworkBehaviour {

    public const int maxHealth = 100;

    [SyncVar(hook = "OnChangeHealth")]
    public int currentHealth = maxHealth;

    //   

    void OnChangeHealth (int health)
    {
        healthBar.sizeDelta = new Vector2(health, healthBar.sizeDelta.y);
    }
}

should be

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;

public class Health : NetworkBehaviour {

    public const int maxHealth = 100;

    [SyncVar(hook = "OnChangeHealth")]
    public int currentHealth = maxHealth;

    // 

    void OnChangeHealth (int health)
    {
        currentHealth = health; // fix
        healthBar.sizeDelta = new Vector2(health, healthBar.sizeDelta.y);
    }
}

Thanks! Will pass to the Learn team.

1 Like