- Home /
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?
Answer by joshpsawyer · Apr 08, 2014 at 04:05 PM
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.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to stop transform movement at a certain point? 1 Answer
Button position 4 Answers
Help with college assignment -2 Answers