Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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

alt text

health.jpg (53.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image masterawai · Nov 10, 2014 at 01:52 AM 0
Share

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");
         }
     }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

"using Android" doesn't recognizable on Unity 1 Answer

Android Development; How to use SDK manager! 1 Answer

Anisprite animation from OnTriggerEnter 0 Answers

My high score do not update after restart 2 Answers

How to share the post to internet from game in unity3d? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges