Search Unity

Host Health

Discussion in 'Multiplayer' started by odd3002, Nov 23, 2019.

  1. odd3002

    odd3002

    Joined:
    May 19, 2019
    Posts:
    1
    Hi guys! I just want to say that I am a complete beginner and I'm trying to make a simple LAN game using UNet, I have a code for the Player Health with a syncvar health:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.Networking;
    7.  
    8. public class PlayerHealth : NetworkBehaviour
    9. {
    10.     public Text healthtxt;
    11.     [SyncVar]public int health = 100;
    12.  
    13.     private void Update()
    14.     {
    15.         healthtxt.text = "HEALTH = " + health;
    16.     }
    17.     private void OnTriggerEnter(Collider other)
    18.     {
    19.        if(other.CompareTag("Sword"))
    20.         {
    21.             TakeDamage(other);
    22.         }
    23.     }
    24.     void TakeDamage(Collider cmd_other)
    25.     {
    26.         health -= cmd_other.gameObject.GetComponent<SwordDamage>().damage;
    27.     }
    28.  
    29. }
    30.  

    the thing is that the host is not updating his health value and neither the text, and I can't figure out why, some help would be very much appreciated
     
    Last edited: Nov 23, 2019
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Add a [ServerCallback] tag to your OnTrigger function. This makes sure that you only modify the health on the server. It should then automatically sync to the client.