Healthbar from PNG

Hello, i have some problems with my healthbar. The problem is when the player loses health the healthbar is shrinking in width and in height, but i only told the script(for what i think i told it) to shrink in the width.

This is my script so far:

var noStyle : GUIStyle;
var bg : Texture2D;
var hpBar : Texture2D;
var hpWidth : int;
function OnGUI() {
	
	noStyle = new GUIStyle();
	
	hpWidth = 228 / (maxHealth / curHealth); // let the bar shrink if the player loses health
	
	GUI.Box(Rect(0,0,500,150), bg, noStyle);
	GUI.Box(Rect(201,33,hpWidth,17), hpBar, noStyle);
	
	
}

You might want to try DrawTexture() instead, because then you can tell it to scale the texture to fit to the Rect you give it. the Box settings are unchangable (at least as far as i know.)

eg:

Code:
var noStyle : GUIStyle;
var bg : Texture2D;
var hpBar : Texture2D;
var hpWidth : int;
function OnGUI() {

noStyle = new GUIStyle();

hpWidth = 228 / (maxHealth / curHealth); // let the bar shrink if the player loses health

GUI.DrawTexture(Rect(0,0,500,150), bg);
GUI.DrawTexture(Rect(201,33,hpWidth,17), hpBar, ScaleMode.StretchToFill);
}

Thanks man, but did you mean ‘StretchToFill’ because ‘StretchToFit’ doesn’t exists? (That was what the unity-error told me)

StretchToFill, I think… you want to fill the entire area

yeah, it’s StretchToFill, sorry! I took a quick look at the script reference to make sure that’s what you wanted, and Fit looks like Fill when you look quickly.

It’s not a problem, I’m happy that someone wanna help me so quick :slight_smile:

I’m GLaD to help! :smile: slight Portal reference… Anyway, I wanted the same thing, and I use the solution to make Health bars, or simulated loading bars. Anyway, good luck on your project!

I’ve one question left, when i click on the play button the texture is in a much lower detail as the real PNG file, how can i solve this?

my PNG file is 500x150 and i told the script to make the texture 500x150 or didn’t I?

Solved it by changing the texture in the inspector. Settings i’ve changed:

Texture type to GUI
Format to Truecolor