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 Heffalus · Feb 25, 2015 at 08:57 PM · c#guiloop

GUI health status

I have a canvas with a panel, in the panel i want to display an image for each healthpoint the player has, max 20.

The code i use for this is:

 float _slots = 0f;
         
         for (int i = 0; i < 2; i++) {
             for (int k = 0; k < 10; k++) {
                 if (_slots <= PlayerHealthPoints) { 
                     GameObject Heart = (GameObject)Instantiate (HeartImage);
                     HeartSlots.Add (Heart);
                     Heart.transform.parent = HeartPanel.transform;
                     Heart.name = "Heart" + i + "," + k;
                     Heart.GetComponent<RectTransform> ().position = new Vector3 (x, y, 0);
                     _slots ++;
                 }
                 x += 50;
                 if (k == 9) {
                     x = 35;
                     y = y - 50;
                 }
             }
         }

I can instantiate and show 20 images fine, the problem is showing less if the player has less health, i tried the if statement but its not working.

Comment
Add comment · Show 2
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 kbaloch · Feb 26, 2015 at 12:07 PM 0
Share

you should try horizontal slider..

avatar image Heffalus · Feb 26, 2015 at 01:05 PM 0
Share

I dont want a slider to represent the health, what i want is images like in the Zelda games.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Haxxxxx · Feb 26, 2015 at 01:40 PM

So I highly suggest making a gameobject with two child images, the first is your overlay the second is your underlay (a grey heart for example).

Then this code should be suitable, attach it your heartPanel:

 public class TEST
 {
     float playerHealth;
 
     float slots = 0f;
 
     float x = 0;
     float y = 0;
     float amountInX;
     float amountInY;
 
     float heartSlots;
     float health;
 
     List<GameObject> HeartSlots = new List<GameObject>();
 
     GameObject heartObject;
 
     float CalculateHealth()
     {
         return heartSlots = health / 20;
     }
 
     void Start()
     {
         for (int i = 0; i < amountInY; i++)
         {
             for (int k = 0; k < amountInX; k++)
             {
                 GameObject Heart = (GameObject)Instantiate(heartObject);
 
                 HeartSlots.Add(Heart);
                 Heart.transform.SetParent(gameObject.transform, false);
                 Heart.name = "Heart" + i + "," + k;
                 Heart.GetComponent<RectTransform>().position = new Vector3(x, y, 0);
                 slots++;
 
                 x += 50;
                 if (k == 9)
                 {
                     x = 35;
                     y = y - 50;
                 }
             }
         }
     }
 
     void UpdateHearts()
     {
         foreach(GameObject heart in HeartSlots)
         {
             heart.gameObject.transform.GetChild(0).GetComponent<Image>().enabled = false;
         }
 
         for (int i = 0; i < heartSlots; i++)
         {
             HeartSlots[i].gameObject.transform.GetChild(0).GetComponent<Image>().enabled = true;
         }
     }
 
     void Update()
     {
         UpdateHearts();
     }
 }

Haven't fully read it through but you get the idea. You'd have to implement bits of it yourself like update the playerHealth etc.

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 Heffalus · Feb 26, 2015 at 06:48 PM 0
Share

Thank you, this worked as i wanted it to :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

How to loop an array of 2d sprites 1 Answer

Multiple Cars not working 1 Answer

Why isn't my simple coroutine working? (and how can I make it infinite?) 2 Answers

What to use for dynamic loading of assets instead of Resources.Load()? 1 Answer


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