Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 darkhog · Apr 15, 2017 at 08:21 PM · healthbarelementsindicator

How to make a healthbar made out of discrete elements?

Think hearts in Zelda or health indicator in Enter the Gungeon.

Comment
Add comment · Show 4
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 Cherno · Apr 15, 2017 at 08:54 PM 0
Share

Well, those are not really bars but indicators consisting of seperate icons, as you noted.

You could implement it in various ways. One would be to have a collection of UI Images and every $$anonymous$$m the player increased the max health of his character, add one element to it. The position of the UI element is easily calculated by taking the index value of the new element multiplied by the icon width plus some padding. When the player loses hit points, access the end of the collection and disable those image components.

avatar image darkhog Cherno · Apr 15, 2017 at 09:19 PM 0
Share

I apreciate your input, but... Shouldn't that be an answer? :) Thanks.

avatar image Bryangs darkhog · Apr 17, 2017 at 12:47 AM 0
Share

@Cherno is right, and sometimes we are not sure if the answer is 100% correct, or the question is vague.

Show more comments

1 Reply

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

Answer by danmartelly_caliper · May 10, 2017 at 01:40 PM

This is how I would set it up

Scene Setup

  1. In your UI Canvas element, make an empty GameObject called "Health bar".

  2. Inside "Health bar" make each of the discrete elements, e.g. "heart1", "heart2", ...

  3. Add a new script component called "HealthTracker" to your "Health bar" game object.

Code

For your "HealthTracker" script have something like this:

 using UnityEngine;
 using UnityEngine.UI;

 public class HealthTracker : MonoBehaviour {

     public Image[] hearts;
     protected int curHealth;
     // max health deteremined by number of heart images

     void Start () {
         curHealth = hearts.Length;
         SetHealth (2);
     }

     void SetHealth(int newHealth) {
         if (newHealth > hearts.Length)
             newHealth = hearts.Length; //can't exceed maximum num of hearts
         for (int i = 0; i < hearts.Length; i++) {
             if (i < newHealth)
                 hearts [i].color = Color.red;
             else
                 hearts [i].color = Color.white;
         }
     }
 }


Put it together

Finally, in the inspector, assign each discrete element to the "hearts" array for the "Health bar" game object. When you use the SetHealth function, it will iterate through the hearts array and change that number of hearts to red and the rest to white.

Comment
Add comment · 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

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

68 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an healthbar 1 Answer

Draw orthographic visuals (e.g. health bar) in perspective scene? 4 Answers

Show changes in healthbar 1 Answer

HealthBar with ColorChange 1 Answer

health bar that scale to the zoom 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