Health Not Working Properly

Hello!

I have a player that has a script attached called “HealthManager” wich contains the players health I want it so that each player has their own health and when they get low that player dies, I know how to add the dieing part but when the player gets damaged it only happens to the server how would I do what I want to do?

Heres my script:

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

public class HealthManager : NetworkBehaviour
{

    public const int maxHealth = 100;

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

    public RectTransform healthBar;

    public void TakeDamage(int amount)
    {
        if (!isServer)
            return;

        currentHealth -= amount;
        if (currentHealth <= 0)
        {
            currentHealth = 0;
            Debug.Log("Dead!");
        }
    }

    void OnChangeHealth(int health)
    {
        //healthBar.sizeDelta = new Vector2(health, healthBar.sizeDelta.y);
        GameObject.Find("Current_Health_Text").GetComponent<Text>().text = "Health: "+currentHealth.ToString();
    }
}

Many thanks in advance!

The thing thats attacking the player is the server

Hi!

You have to update the currentHealth value in the function OnChangeHealth :slight_smile:

how would i go about doing this @SuperNeon

I did it @SuperNeon

1 Like

hehe cool :slight_smile: