Platformer Health Icons

I’m sorry if this has been asked before, I couldn’t find an answer to my specific question/issue.

image

I want the player to have a visual indication to how much health they have (in my case, max health of 4). Each time a player takes damage, the icon turns grey and when they touch a certain pickup they gain the health back, turning the icon back green. My game is based on the 2d platformer project. My current issues is that I can’t seem to get the icons to render in the UI as sprites. I’m currently using image components for prototyping to figure out where they should be. I want them to be sprites so I can animate them as players take damage or recover. Any ideas/recommendations?

Let me know if any more information is needed, and thanks in advance for any help!

Hi,

You could create a HealthDisplay class which references your Image components which reference the circle sprite. Whenever the health value changes, the HealthDisplay component could change the sprites in the referenced Image components. Have you already tried that?

Also please feel free to ask our helpful community of students for further advice and ideas over on our Discord chat server.

Good luck! :slight_smile:


See also:

Hello,

So the way I have it currently set up, I have a HealthDisplay class with the following code:

public class HealthDisplay : MonoBehaviour
{
[SerializeField] private GameObject healthOrb;

 public void SetHealth(int health)
{
    GameObject newHealth;
    float horOffset = 64.0f;
    Vector3 healthPos;

    for (int i = 0; i < health; i++)
    {
        healthPos = gameObject.transform.position;
        healthPos.x = (horOffset * i) + 35.0f;

        newHealth = Instantiate(healthOrb, healthPos, Quaternion.identity) as GameObject;
        newHealth.transform.parent = gameObject.transform;
    }
 }

So the circle sprites are being created at runtime. I thought about putting all the health objects in an array, that way I can reference them whenever the player gets hit, otherwise, I don’t think I can modify any individual object values once they’re instantiated, right? Or would a better approach be to have the gameObjects childed to the canvas in the UI and load them in a variable at startup?

As long as you have a reference to those objects, you can modify them during runtime.

It depends on your game. If you have a fixed or maximum number of “lives” or “health points” that you want to display, you could simply create your design and reference the things you did in your HealthDisplay component.

If you don’t know the maximum number, you’ll probably have to create things during runtime.

Bear in mind, though, that recreating and destroying the same objects over and over again is not good practice because this has got an negative impact on the performance of your game. Ideally, you should reuse what you already created.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms