I have certain amount of sprites in my scene. It is a 2D Game. I want to display some text on the sprite each Sprite's GameObject is attached with a Script. In the Script I have the following OnGUI() Method
void OnGUI ()
{
GUI.contentColor = Color.black;
Rect label = new Rect ();
label.x = worldToRect (transform.position).x;
label.y = worldToRect (transform.position).y;
label.width = 100;
label.height = 100;
GUI.Label (label, number.ToString ());
}
Vector2 worldToRect (Vector3 WorldPosition)
{
Vector2 v = Camera.main.WorldToScreenPoint (WorldPosition);
return GUIUtility.ScreenToGUIPoint (v);
}
But the position is not quite right. The two white Sprites should display 3 at the centre.I checked their transform in the editor and they align exactly at the centre of the sprite. How do I correct this ? 