- Home /
No response, and made progress already so I'm just trying to solve it myself.
UNET - UI Not updating with Healthbar and other bars.
Hello,
So when I run the game, the Health and such will go down in the inspector, but it wont on the UI Healthbar, and the 2 other bars( thirst and food ). Is there an easy fix for this problem? Or does it involve a full remake of this script ( Singleplayer works fine, converting to Multiplayer).
All suggestions welcome, I'd happily try them out.
Thanks for reading.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Networking;
public class Player_Health : NetworkBehaviour
{
private GameManager_Master gameManagerMaster;
private Player_Master playerMaster;
public float playerHealth = 100f;
public Text healthText;
public GameObject deathCanvas;
public float hunger = 100f;
public float thirst = 100f;
public Image hungerBar;
public Image thirstBar;
public Image healthBar;
public float hungerSpeedMultiplier = 0.10f;
public float thirstSpeedMultiplier = 0.25f;
public float playerHealthSpeedMultiplier = 0.30f;
private bool isDying = false;
public Vector3 spawnPoint = Vector3.zero;
void OnEnable()
{
SetInitialReferences();
SetUI();
playerMaster.EventPlayerHealthDeduction += DeductHealth;
playerMaster.EventPlayerHealthIncrease += IncreaseHealth;
}
void OnDisable()
{
playerMaster.EventPlayerHealthDeduction -= DeductHealth;
playerMaster.EventPlayerHealthIncrease -= IncreaseHealth;
}
void Start()
{
StartCoroutine(TestHealthDeduction()); //deathtest
deathCanvas.SetActive(false);
}
private void Update()
{
CheckDeath();
if (hunger > 0)
{
hunger -= Time.deltaTime * hungerSpeedMultiplier;
}
if (thirst > 0)
{
thirst -= Time.deltaTime * thirstSpeedMultiplier;
}
if (hunger <= 0 || thirst <= 0 || hunger <= 0)
{
isDying = true;
}
else
{
isDying = false;
}
if (isDying == true)
{
playerHealth -= Time.deltaTime * playerHealthSpeedMultiplier;
}
if(hunger > 100)
{
hunger = 100f;
}
if (thirst > 100)
{
thirst = 100f;
}
healthBar.fillAmount = playerHealth / 100;
hungerBar.fillAmount = hunger / 100;
thirstBar.fillAmount = thirst / 100;
}
private void CheckDeath()
{
if (playerHealth <= 0)
{
Die();
}
}
void SetInitialReferences()
{
gameManagerMaster = GameObject.Find("Game.Manager").GetComponent<GameManager_Master>();
playerMaster = GetComponent<Player_Master>();
}
IEnumerator TestHealthDeduction()
{
yield return new WaitForSeconds(2);
DeductHealth(20); // Removes health *@**@*@*@**@*@*@*@
yield return new WaitForSeconds(4);
DeductHealth(20); // Removes health *@**@*@*@**@*@*@*@
}
void DeductHealth(int healthChange)
{
playerHealth -= healthChange;
if(playerHealth <=0)
{
playerHealth = 0;
Die();
}
SetUI();
}
private void Die()
{
deathCanvas.SetActive(true);
gameObject.SetActive(false);
}
public void Respawn()
{
playerHealth = 100;
hunger = 100;
thirst = 100;
deathCanvas.SetActive(false);
transform.position = spawnPoint;
gameObject.SetActive(true);
}
public void AddHunger(float amount)
{
hunger += amount;
}
public void AddThirst(float amount)
{
thirst += amount;
}
void IncreaseHealth(int healthChange)
{
playerHealth += healthChange;
if(playerHealth > 100)
{
playerHealth = 100;
}
SetUI();
}
void SetUI()
{
if(healthText !=null)
{
healthText.text = playerHealth.ToString();
}
}
}
I am having the same problem. I am making a shooter game, and every player has a health bar above him.
When a player shoots at an enemy, the fillAmount of the health bar of the enemy decreases successfully, but it is not updating in the enemy's window.
Have you found a solution ?
I know you closed the question, but I really need help. Thanks.
Answer by SergioSandiaz · Sep 19, 2016 at 06:00 AM
Why don´t you use a slider?
slider.value = playerHealth;
I see your using images and I don´t understand why
Image uses fill amount, so it reduces in size when the int does.
The image and ints just don't update with the UI and the health ints.
I think it has to do with the networking, any clue?