[Solved]HealthBar Not updating

Updated on May 19, 2017 in [A] Multiplayer
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
2 on May 12, 2017

Hi so I’ve tried implementing a healthbar from a different youtuber in with the brackeys fps tutorial and am having trouble getting it to work properly. It’s three scripts interacting, so I know this will be a long post. I’m sorry, I wasn’t sure how to make it clearer, so I bolded and underlined what areas I was having trouble with, but left in others in case it’s something I’m overlooking.

 

Basically I got the CurrentVal in stat (script 3) working with the rpctakedamage in player (script 1). So if the player looses 10 health the CurrentVal changes to 90 (down from 100). I’m having trouble with the healthbar on the GUI, it’s not updating to this info. The function I have for maps (in barscript, script 2)works with manual changing of the fill amount slider in the inspector, but it doesn’t represent the players real health.

 

I’m pretty sure it’s something really simple and obvious, I just keep getting close, but not close enough.

 

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

public class Player : NetworkBehaviour {

[SyncVar]
private bool _isDead = false;
public bool isDead

{
get { return _isDead ; }
protected set { _isDead = value; }
}

public bool isHit;

public BarScript skills;

[SerializeField]
private Stat health;

[SerializeField]
private float maxHealth = 100;

[SyncVar]
private float currentHealth;

[SerializeField]
private Behaviour[] disableOnDeath;
private bool [] wasEnabled;

private void Awake()
{

currentHealth = health.CurrentVal;
health.Initialize();

}

public void Setup ()
{
if (isLocalPlayer)
{

}
CmdBroadCastNewPlayerSetup ();

}

[Command]
private void CmdBroadCastNewPlayerSetup()
{
RpcSetupPlayerOnAllClients ();

}

[ClientRpc]
private void RpcSetupPlayerOnAllClients ()
{
wasEnabled = new bool[disableOnDeath.Length];
for (int i = 0; i < wasEnabled.Length; i++)
{
wasEnabled[i] = disableOnDeath[i].enabled;
}

SetDefaults ();
}

void Update ()

{
// if (!isLocalPlayer)
// return

if (Input.GetKeyDown(KeyCode.K))
{
RpcTakeDamage(10);
}
}

[ClientRpc]
public void RpcTakeDamage(float _amount)

{
if (isDead)
return;

health.CurrentVal -= _amount;

Debug.Log(transform.name + “now has” + health.CurrentVal + “health.”);

if (health.CurrentVal <= 0)
{
Die();
}
}

private void Die()
{
isDead = true;

for (int i = 0; i < disableOnDeath.Length; i++)
{
disableOnDeath[i].enabled = false;
}

Collider _col = GetComponent ();
if (_col != null)
_col.enabled = false;

//Disable components

Debug.Log(transform.name + “is DEAD!”);

StartCoroutine (Respawn());

//call respanw method
}

private IEnumerator Respawn ()
{
yield return new WaitForSeconds( GameManager.instance.matchSettings.respawnTime);

SetDefaults ();
Transform _spawnPoint = NetworkManager.singleton.GetStartPosition ();
transform.position = _spawnPoint.position;
transform.rotation = _spawnPoint.rotation;

Debug.Log (transform.name + “Respawn”);
}

public void SetDefaults ()
{

isDead = false;
health.CurrentVal = maxHealth;
health.Initialize();

for (int i = 0; i < disableOnDeath.Length; i++)
{
disableOnDeath[i].enabled = wasEnabled[i];
}

Collider _col = GetComponent ();
if (_col != null)
_col.enabled = true;
}

}

 

Other script

 

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

[System.Serializable]
public class BarScript : NetworkBehaviour {

[SerializeField]
private float fillAmount;

[SerializeField]
private Image content;

public float MaxValue { get; set;}

public float Value
{
set
{
fillAmount = Map (value, 0, MaxValue, 0, 1);
}
}

void Update ()

{
HandleBar ();

private void HandleBar()
{
if (fillAmount != content.fillAmount)
{
content.fillAmount = fillAmount;
}
}

private float Map(float value, float inMin, float inMax, float outMin, float outMax)

{
return (value- inMin) * (outMax – outMin) / (inMax – inMin) + outMin;
}
}

 

last script

 

using UnityEngine;
using System.Collections;
using System;

[Serializable]
public class Stat
{
[SerializeField]
private BarScript bar;

[SerializeField]
private float maxVal;

[SerializeField]
private float currentVal;

public float CurrentVal
{
get
{
return currentVal;
}

set
{
this.currentVal = value;
bar.Value = currentVal;

}
}

public float MaxVal
{
get
{
return maxVal;
}

set
{

this.maxVal = value;
bar.MaxValue = maxVal;

}

}

public void Initialize()
{
this.MaxVal = maxVal;
this.CurrentVal = currentVal;
}
}

  • Liked by
Reply
0 on May 12, 2017

Any help would be awesome thank you.

  • Liked by
Reply
Cancel
0 on May 19, 2017

Solved this a few days after asking. For anyone interested, this is what I did.

 

The reference of the health wasn’t working properly like this, because the ui was not part of the player. The other way around it would be to reference gameobject that the healthbar was referring to in the script. So doing either one of those things works to fix it. 

  • Liked by
Reply
Cancel