Texture2D width


I’m programming HP bar for the bad guys of my little game and I’ve run across two problems that I can’t solve by myself or by googling. I hope you’ll be able to help me. It might be important to know that my hp bars appear on the top of the screen kinda like in Diablo and not above their head.

First problem :

When I change the bar width it moves to the left for no apparement reason. This make it go outside of the frame (which is also a Texture2D but it doesn’t move).

Second problems :

My HP bars show up when an enemy it being hit and disapear after a while if nothing happened to this enemy. The big problem I have is how do I switch bar, because if there is two enemies I want the bars to switch depending on who I hit. Right now they just appear on top of each other. So I’d like to hide one without destroying it every time this happens so I only have one bar on the screen. The bars have to keep the information once they are hidden obviously.

Here is the GUI part of the script attached to each bad guy :

void OnGUI() {
		if (hasBeenhit == true && CoolDownGUI == true) {
			FramePosition.x = (Screen.width - FramePosition.width) / 2;
			FramePosition.width = Screen.width * 0.4f;
			GUI.DrawTexture (FramePosition, Frame);
			HealthBarPosition.x = FramePosition.x + FramePosition.width * OffsetHori;
			HealthBarPosition.y = FramePosition.y + FramePosition.height * OffsetVerti;
			HealthBarPosition.width = FramePosition.width * healthPourcent;
			GUI.DrawTexture (HealthBarPosition, HealthBar);