- Home /
Segmented health bar with health amount selectable by player
I have a simple health system where the player can take a set number of hits before dying. The player is able to choose how many hits they can take and I would like to be able to reflect their choice in the health bar.
Right now my health bar is a single image that shrinks from right to left as the player takes damage (see below). I would like to instead visualize the number the player chooses with each hit point as a separate block. As the player takes damage the right-most block would disappear.
I think that what I need to do is get the health number the player has selected in Start and then instantiate the correct number of child objects inside my container, but the width and spacing of each would need to be based on the number chosen by the player.
Does anyone know how I go about adding the child blocks in the correct positions and with the right widths and spacing?
Hopefully the image below illustrates what I have now and what I am trying to achieve:
Answer by glasshalfpool · May 26, 2021 at 09:57 PM
I found several ways of doing this, but ended up creating a sprite sheet with versions of the health bar from 1 - 20.
I sliced this up using the sprite editor and put it on a game object that was the parent of my main health bar then added a mask to it. I had already written code so that damage to the player makes the health bar shrink the appropriate % so the reduced health bar lines up perfectly with the segments in the mask.
To select the correct image for the player's chosen amount of health I created a sprite array as described in a great article on gamedevbeginner.com
Your answer
![](https://koobas.hobune.stream/wayback/20220613034002im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Chat Message Box with Dynamic Message Sizes help 1 Answer
novice: dynamically load xml or text and populate textfield (quiz style game) 1 Answer
How to get a Text child, when there are many Text childs 4 Answers
How to prevent child GameObjects from inheriting Button onHover command?(wording this wrong) 0 Answers
Scroll view not resizing properly with dynamic content 0 Answers