Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411281 Posts in 69324 Topics- by 58380 Members - Latest Member: bob1029

March 28, 2024, 10:13:23 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)NGUI Progress bar health doesn't move when hit by the enemy
Pages: [1]
Print
Author Topic: NGUI Progress bar health doesn't move when hit by the enemy  (Read 662 times)
MNQ
Level 0
*


View Profile
« on: November 09, 2014, 06:56:00 AM »

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.

Code:
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?

Code:
void Update()
     {
                 
                 GameObject.Find ("PROGRESS").GetComponent<UISlider> ().sliderValue = HealthScript;
     }
 

Logged
DocProctopus
Level 0
**



View Profile WWW
« Reply #1 on: November 09, 2014, 09:51:53 PM »

There's a few issues here, but if we're talking specifically about your GUI problem, the issue is that you are trying to assign the type HealthScript to UISlider.sliderValue, rather than assigning the value of HealthScript.hp. I haven't ever used NGUI, but in your second script there, this should work:

Code:
private HealthScript health;
private UISlider healthBar;

void Start()
{
    health  = HealthScript.instance;
    healthBar = GameObject.Find("PROGRESS").GetComponent<UISlider>();
}

void Update()
{
    healthBar.sliderValue = health.hp;
}

Here's what I've done:
  • Added a reference to your HealthScript instance as a private member of your second script. It looks like HealthScript is supposed to be a Singleton, so I've made use of what you have, but I'll talk more about this in a moment.
  • Added a reference to the UISlider component so that you don't have to call GameObject.Find() every Update. Your way will work, but this is more efficient.
  • In Update, you are now setting the sliderValue to HealthScript.hp instead of simply to HealthScript, which should fix the problem in the title of your post (although I haven't tested this).

These points aren't directly related to your question, but may help you out:

  • It looks like you have half-implemented HealthScript as a Singleton, which is a class which can only have a maximum of one instance. By your implementation, however, you can create HealthScripts all over the place, and the static variable instance will always refer to the one that was most recently created. As a general guideline, I would say that if you want to make a class a Singleton, it should not be a MonoBehaviour, because you cannot call constructors on MonoBehaviours, which makes them much harder to implement. If you're interested in Singletons you should read http://gameprogrammingpatterns.com/singleton.html.
  • HealthScript is taking on too many unrelated responsibilities. In particular, if you have a script just for the player's health, you should put the code for high scores in its own separate script so that it's easier to find. If HealthScript is supposed to generally refer to the player, I would recommend renaming it.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic