Obtaining sprite 2D position on screen

Hello.

I have an issue with my sprites. I’m trying to draw some text over a sprite using a GUI.Label present in the sprite’s script.

However, when I try to draw it:
GUI.Label(new Rect(transform.position.x + 1,transform.position.y + 1, 400, 40), “etcetc”);

The text is drawn in the top left, at the position 1,1 which means the transform.position is null.

What exactly do I have to use to get the exact 2D positioning of a sprite on screen?

It’s likely the issue you have is not that the transform is null (which would throw an error), but rather you’re not converting world space to screen space.

You’ll want to convert your transform.position to screen space using Camera.main.WorldToScreenPoint().

Specifically, create a new variable screenPosition and assign to it Camera.main.WorldToScreenPoint(transform.position), then access screenPosition.x and screenPosition.y.