Positioning a GUI.Label inside a prefab

I have a prefab that, at times, will be on the screen multiple times. Each prefab needs text positioned on it. I’m using GUI.Label for showing the text, how can I show it at an offset from within the prefab? I have created a script that is attached to the prefab itself and I’m drawing the label in the OnGui event. I was really hoping drawing it at 0,0 would put it at the top-left of the prefab but no such luck :(.

Any guidance here would be appreciated. I’m sure it’s simple, but I didn’t want to jury rig a half-measure and I couldn’t find this specific information from searching.

– edit –

I’m not sure I have my prefab set up correctly, it feels like it’s incomplete. I don’t know if I’m supposed to put a collider on it? But I don’t think it technically has a size.

Here’s a screenshot. The rectangular box at the top is my “team view” and the label that is painted near the bottom of the window is supposed to be showing in the dark gray bar at the upper portion of the teamview prefab.


Simple way, it’s use function WorldToScreenPoint(), which transforms position from world space into screen space. As your script is attached to prefab, for example(write on CSharp):

 void OnGUI() {
  //Use Main Camera and get position current object, but point position is pivot point
  Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position);
  //If pivot point current object at center, we offset it a little. For example 10
  //And size label, for example 100x50
  GUI.Label(new Rect(screenPos.x, Screen.height - screenPos.y, 100, 50), "myLabel");

I hope that it will help you.