Hi, I’m trying to make a very simple way to pass the health of the player to ngui as a float and adjust the hp bar. The health of the player seems to work, as does adding a float manually to ngui changes the size of the life bar but I can’t seem to figure out how to connect the two to check on its own.
I have this script PlayerHealth.cs
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
private int maxHealth = 300;
public int health;
// Use this for initialization
void Start () {
health = maxHealth;
}
// Update is called once per frame
void Update () {
gameObject.SendMessage("AlterHealth", .5f);
}
}
And I have this as VitalBarBasic.cs
using UnityEngine;
using System.Collections;
public class VitalBarBasic : MonoBehaviour {
private UISlider _slider;
private float _maxWidth;
private float _health;
void Awake() {
_slider = GetComponent<UISlider>();
_maxWidth = _slider.foreground.localScale.x;
}
void Start() {
}
public void Update() {
_slider.sliderValue = _health;
}
public void UpdateDisplay( float x ) {
_slider.sliderValue = x;
}
public void AlterHealth(int newHealth){
_health = newHealth;
}
}
I don’t think I have the script actually looking in the right place but I’m a little confused how to structure the “sendmessage” Thanks for any help.