- Home /
Question by
masterawai · Nov 09, 2014 at 05:03 PM ·
c#androidngui
NGUI Progress bar health doesn't move when hit by the enemy
I want to make health system using the NGUI. I got the idea, but i don't know how interpret into coding. but so far, this is my progress.
using UnityEngine;
public class HealthScript : MonoBehaviour
{
public static HealthScript instance;
public int hp = 1;
private GUIText scoreReference;
private GUIText highscoreReference;
private static int _highscore = -1;
public int highscore {
get { if (_highscore == -1)
_highscore = PlayerPrefs.GetInt("Highscore", 0);
return _highscore;
}
set {
if (value > _highscore) {
_highscore = value;
highscoreReference.text = _highscore.ToString();
PlayerPrefs.SetInt("Highscore", _highscore);
}
}
}
public bool isEnemy = true;
private static int points;
public void Damage(int damageCount) {
hp -= damageCount;
if (hp <= 0)
{
// Dead!
Destroy(gameObject);
points++;
scoreReference.text = points.ToString();
}
}
public void gameEnd() {
highscore = points;
points = 0;
}
void Start()
{
scoreReference = GameObject.Find("Score").guiText;
highscoreReference = GameObject.Find("HighScore").guiText;
scoreReference.text = points.ToString();
highscoreReference.text = highscore.ToString ();
instance = this;
}
then this is my update method, how should i do?
void Update()
{
GameObject.Find ("PROGRESS").GetComponent<UISlider> ().sliderValue = HealthScript;
}
this is the game interface
health.jpg
(53.0 kB)
Comment
Answer by SquigglyFrog · Nov 09, 2014 at 09:59 PM
I might be missing something, but shouldnt you be setting the sliderValue to an INT or FLOAT? NOT to a script?
Should be something like this I would think:
GameObject.Find ("PROGRESS").GetComponent ().sliderValue = HealthScript.hitPoints;
emm.. but i'm changed the code. someone suggest this, but still doesn't work.
public UISlider _healthBar;
void Start()
{
scoreReference = GameObject.Find("Score").guiText;
highscoreReference = GameObject.Find("HighScore").guiText;
scoreReference.text = points.ToString();
highscoreReference.text = highscore.ToString ();
instance = this;
GameObject progressObj = GameObject.Find("PROGRESS");
if (progressObj == null) {
Debug.LogError("$$anonymous$$issing progress object");
return;
}
_healthBar = progressObj.GetComponent<UISlider>();
if (_healthBar == null) {
Debug.LogError("Progress Object missing UISlider");
}
}