GuiTexture above 3D objects

Hello,

I’m trying to put health bars above my bad guys and I want the bar to follow them and also always face the camera. I’ve watched and read a few tutorials but half of them don’t work, maybe it’s because I’m using the GUI.DrawTexture wrong.

So far I have this code which is attached to my gameobject GUITexture in the game (I basically found this code in the forum) :

public Texture myText;
public Transform target;
float x, y;
public float xOffset, yOffset;

// Update is called once per frame
void Update () {
	Vector3 BarPosition;
	BarPosition = Camera.main.WorldToScreenPoint (target.position);
	x = BarPosition.x - xOffset;
	y = Screen.height - BarPosition.y - yOffset;
	Rect HPBAR = new Rect (x, y, 100, 20); // 100 and 20 Random values to test
	GUI.DrawTexture (HPBAR, myText);
}

With this nothing shows up, whatever xOffset and yOffset might be. Target is supposed to by my bad guy and Texture is obviously my texture.

Any ideas ?

First off, GUI.DrawTexture() is not the same thing as a GUITexture. The reason your GUI.DrawTexture() is not showing up is that GUI commands must be executed in the OnGUI() callback. So you are going to have to do:

void OnGUI() {
    Rect HPBAR = new Rect (x, y, 100, 20); // 100 and 20 Random values to test
    GUI.DrawTexture (HPBAR, myText);
}