Hi, thank you for reading this post.
I have a very simple but utterly annoying problem. Im using the Unity health script from the documentation. It works, but how do I use the player
s currentHealth variable in a text element that is a child of it? So I can display it. I tried a million different ways which all failed. The ways I tried kinda stacked the text of all players over each other. I just want the player to see his own health on its screen and for the other players to see their health on their screen.
It may be a noob question but I couldn`t for the life of me find an answer.
The used health script:
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;
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);
}
}