Display UI Icon next to GameObject

Hi, so it’s my first time using the forum so I’ll be brief. I want to display an icon that represents health of a current creature on the board. The code it’s pretty extensive so I’ll show which pieces I’m using for this.

BaseCreature script

using UnityEngine;

public class BaseCreature : MonoBehaviour
{
    public int HealthStat;
    public int CurrentHealth;

    private void Awake()
    {
        this.CurrentHealth = this.HealthStat;
    }

    protected virtual void Start()
    {
       
        var healthUI = Instantiate(CurrentHealthUI);
        healthUI.GetComponent<ShowHealth>().CreatureInfo = this;
    }

And this is the code for the Image itself:

using UnityEngine;
using UnityEngine.UI;

public class ShowHealth : MonoBehaviour
{
    public BaseGeneral CreatureInfo;

    private GameObject Canvas;
    public GameObject Text;

    private void Awake()
    {
        this.Canvas = GameObject.Find("Canvas");
        this.transform.SetParent(Canvas.transform, false);      
    }

    private void Start()
    {
        var spriteRenderer = CreatureInfo.GetComponent<SpriteRenderer>();
        var leftCornerX = CreatureInfo.gameObject.transform.position.x - (spriteRenderer.bounds.size.x / 2);
        var leftCornerY = CreatureInfo.gameObject.transform.position.y - (spriteRenderer.bounds.size.y / 2);
        this.transform.position = transform.TransformPoint(new Vector2(leftCornerX, leftCornerY ));
    }

    private void Update()
    {
        this.Text.GetComponent<Text>().text = this.CreatureInfo.CurrentHealth.ToString();
    }
}

This is the result:
7544071--932188--GameCapture.PNG

So I thought my calcs were bad (they are btw), so I tried to move its position so it matches better but I don’t get it to move even a little bit. I was reading documentation and looking for someone who had this trouble, even examples but I didn’t found any one in 2D or that had that problem. So I’m looking forward about a solution for this problem and maybe and example of how to implement it. Thanks for reading :slight_smile:

Hey,

Can you try changing line 23 to this: transform.position = new Vector2(leftCornerX, leftCornerY));.

transform.position is already the world space position, so you don’t need to use transform.TransformPoint().

If it’s not that, make sure that the bounding box of the sprite is actually hugging the sprite image.

Also, make sure that the sprite Pivot is set to “Center” in the sprite’s inspector.

Let me know if this works, good luck!

Hi again, so, I think I missed a really important part. The board and creatures are not part of UI, that’s why I used TransformPoint, bc otherwise it shows up like this:


And, if I’m not wrong, that’s bc that corner is where local or scene position actually is. Board position is hardcoded and order one following the other by an algorithm I made:

And 'bout sprite bounds … idk if I’m getting there in the right way I should recieve them and I think’s that’s the problem. When I get the values by debugging in line 21 and 22 I get float values, those from scene position (0.7 and 1.0 for x and y respectively) … but I think I should be receiving them like pixels? I can’t know since I’m not that into UI elements. Anyways, I’ll keep trying and tell if I found the solution, ty for your help :slight_smile:

Hey,

So I did miss an important part; my previous suggestion was based on the assumption that nothing was part of the UI, which was my mistake. Pixelated text like that is obviously part of the UI and I missed that.

UI elements are not using local or world space by default, they’re using screen space. You need to convert the creature’s world space coordinates to screen space coordinates, and then set the health icon’s position to those screen space coordinates.

You can accomplish this by using Camera.WorldToScreenPoint().

I have modified your start method so that it should work now:

    private void Start()
    {
        var spriteRenderer = CreatureInfo.GetComponent<SpriteRenderer>();
        var leftCornerX = CreatureInfo.gameObject.transform.position.x - (spriteRenderer.bounds.size.x / 2);
        var leftCornerY = CreatureInfo.gameObject.transform.position.y - (spriteRenderer.bounds.size.y / 2);

        Camera cam = FindObjectOfType<Camera>();
        Vector3 screenPosition = cam.WorldToScreenPoint(new Vector2(leftCornerX, leftCornerY));

        transform.position = screenPosition;
    }

I haven’t tested this exact code, so there might be a mistake somewhere. Let me know if it doesn’t work. Also, I did this by making as few changes as possible. You can optimize the code as you like.


OR

You can just use 3D text for your health icons and bypass all of the above since 3D text is not a UI element:

OR

You can use a World Space canvas.


Let me know how this goes, and good luck!

1 Like

Thank you, but I’m afraid to said that I figured out yesterday … since it was like 2 a.m. in my country I forgot to reply that, yup, I was wrong and I should been using Screen Space. I’ll try that solution of 3D Text, if I can bypass all that point conversion stuff then I’d be glad.

If I have any trouble implementing that I’ll probably make another question, but for now I’ll mark this post as resolver. Ty for your help. :slight_smile: (Btw I’d post the result since also the formula was wrong, it should be x - x/4 + r, but I’m not at home rn)

1 Like

Glad you got it working but please note, the appropriate place to ask about UI questions are the dedicated UI forums.

1 Like

Oh, I didn’t know, ty for the correction. Then just bc I don’t want to make the same mistake, what themes are correctly taged as 2D? Also which tags or where can I see which tags for posts (tags, not subforum) can I add?

Technically the 2D forum is about features put out by the 2D Team so Sprites, 2D physics, Tilemaps etc but also questions about 2D game making in general is fine too where it wouldn’t be appropriate elsewhere.

UI, Particles, Scripting (mostly devs who don’t understand Syntax errors), Editor, Packages, Prefabs, Timeline, Visual Scripting, AI & Navigation are common posts here, all of which have their own dedicated sub-forums and are not something the 2D team maintain.

Often we get posts here simply because the dev is simply making a 2D game but the subject isn’t actually anything to do with making a 2D game specifically, often it’s how to use the UI system in general for instance.

It’s not a holy sin to post here in the above case and we’re not trying to be hardcore forum police, it’s just that we like to encourage the use of the dedicated forums is all. You’re likely to get an answer from someone who’s visiting that forum with more knowledge on the subject :slight_smile:

1 Like