Search Unity

UNET Documentation

Discussion in 'Documentation' started by Deleted User, Jul 22, 2017.

  1. Deleted User

    Deleted User

    Guest

    https://unity3d.com/ru/learn/tutorials/topics/multiplayer-networking/networking-player-health

    Missed assigning of the SyncVar value in the hook

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5.  
    6. public class Health : NetworkBehaviour {
    7.  
    8.     public const int maxHealth = 100;
    9.  
    10.     [SyncVar(hook = "OnChangeHealth")]
    11.     public int currentHealth = maxHealth;
    12.  
    13.     //  
    14.  
    15.     void OnChangeHealth (int health)
    16.     {
    17.         healthBar.sizeDelta = new Vector2(health, healthBar.sizeDelta.y);
    18.     }
    19. }

    should be

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5.  
    6. public class Health : NetworkBehaviour {
    7.  
    8.     public const int maxHealth = 100;
    9.  
    10.     [SyncVar(hook = "OnChangeHealth")]
    11.     public int currentHealth = maxHealth;
    12.  
    13.     //
    14.  
    15.     void OnChangeHealth (int health)
    16.     {
    17.         currentHealth = health; // fix
    18.         healthBar.sizeDelta = new Vector2(health, healthBar.sizeDelta.y);
    19.     }
    20. }
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Thanks! Will pass to the Learn team.
     
    Deleted User likes this.