Author Topic: NGUI Progress bar health doesn't move when hit by the enemy  (Read 3003 times)

masterawai

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
NGUI Progress bar health doesn't move when hit by the enemy
« on: November 09, 2014, 02:19:00 PM »
I want to make health system using the NGUI. But, it didn't work, the health bar doesn't moving. Here is part of my Healthscript code.

  1. using UnityEngine;
  2.  
  3. public class HealthScript : MonoBehaviour
  4. {
  5.  
  6.         public static HealthScript instance;
  7.         public int hp = 1;
  8.         private GUIText scoreReference;
  9.         private GUIText highscoreReference;
  10.         private static int _highscore = -1;
  11.         public int highscore {
  12.                 get { if (_highscore == -1)
  13.                         _highscore = PlayerPrefs.GetInt("Highscore", 0);
  14.                         return _highscore;
  15.                 }
  16.                 set {
  17.                         if (value > _highscore) {
  18.                                 _highscore = value;
  19.                                 highscoreReference.text = _highscore.ToString();
  20.                                 PlayerPrefs.SetInt("Highscore", _highscore);
  21.                         }
  22.                 }
  23.         }
  24.        
  25.         public bool isEnemy = true;
  26.        
  27.        
  28.         private static int points;
  29.         public void Damage(int damageCount) {
  30.                 hp -= damageCount;
  31.                
  32.                 if (hp <= 0)
  33.                 {
  34.                         // Dead!
  35.                         Destroy(gameObject);
  36.                         points++;
  37.                         scoreReference.text = points.ToString();
  38.                        
  39.                        
  40.                 }
  41.         }
  42.        
  43.         public void gameEnd() {
  44.                
  45.                 highscore = points;
  46.                 points = 0;
  47.         }
  48.  
  49.         UISlider _healthBar;
  50.         void Start()
  51.         {
  52.  
  53.                 scoreReference = GameObject.Find("Score").guiText;
  54.                 highscoreReference = GameObject.Find("HighScore").guiText;
  55.                 scoreReference.text = points.ToString();
  56.                 highscoreReference.text = highscore.ToString ();
  57.                 instance = this;
  58.  
  59.                 GameObject progressObj = GameObject.Find("PROGRESS");
  60.                 if (progressObj == null) {
  61.                         Debug.LogError("Missing progress object");
  62.                         return;
  63.                 }
  64.                
  65.                 _healthBar = progressObj.GetComponent<UISlider>();
  66.                 if (_healthBar == null) {
  67.                         Debug.LogError("Progress Object missing UISlider");
  68.                 }
  69.        


it's had an error at the NGUIEditorTools script, the NGUI provided.  It's said default perimeter specifier are not permitted. Hope you guys can help me..thanks


  1. static public bool DrawHeader (string text, bool forceOn = false) { return DrawHeader(text, text, forceOn); }




ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI Progress bar health doesn't move when hit by the enemy
« Reply #1 on: November 09, 2014, 09:22:19 PM »
Why would it move? You only have initialization code, and nothing else. No Update, no LateUpdate, no UIFollowTarget, nothing. How do you expect it to move?

Furthermore, why are you using GUIText? That's not NGUI. In fact, I don't even see you use ANY NGUI code here... You really should start off by watching NGUI's beginner tutorials.

Easiest way to have UI follow some target is to anchor a widget to it. Alternative is to use UIFollowTarget script from the HUDText add-on.