I’m new to coding, and trying to build a very very basic health system with NGUI and Unity. For the moment I’m trying just trying to get the Progress Bar to update the current HP I have on the player, which I have as an int. I have 2 separate codes, 1 on the gui, and 1 on the player which I’ll add damage to, etc. For the moment I’m just trying to link them and having issues figuring it out.
I know this is very bad, but I’m trying to learn, not copy/paste. So far I have this.
PlayerHealth (attacked to player)
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
private int maxHealth = 300;
public float Health;
// Use this for initialization
void Start () {
Health = maxHealth;
}
// Update is called once per frame
void Update () {
}
}
gui code
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() {
_slider = GetComponent<UISlider>();
_health = Player.GetComponet<Health>();
}
public void Update() {
_slider.sliderValue = _health/30;
}
public void UpdateDisplay( float x ) {
_slider.sliderValue = x;
}
}