Unity Health Bar GUI insertion help

So I was watching one of burgzergarcade’s videos where I found out how to make a healthbar. Really helped but I wanna know how to add my own gui texture to it. Can someone explain to me how, checked the references but got confused :confused:
Thanks in advance!

using UnityEngine;
using System.Collections;

public class PlayerHealth1 : MonoBehaviour {
	public int maxHealth = 100;
	public int curHealth = 100;
	
	
	public float healthBarLength;

	// Use this for initialization
	void Start () {
		healthBarLength = Screen.width / 4;
	}
	
	// Update is called once per frame
	void Update () {
		AdjustCurrentHealth(0);
	}
	
	void OnGUI(){
		GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
	}
	
	public void AdjustCurrentHealth(int adj){
		curHealth += adj;
		
		if(curHealth < 1)
			curHealth = 0;
		
		if(curHealth > maxHealth)
			curHealth = maxHealth;
		
		if(maxHealth < 1)
			maxHealth = 1;
		
		healthBarLength = (Screen.width / 4) * (curHealth / (float)maxHealth);
	}
}

A pic of the gui texture

There are two solutions:

  1. Use GUI.Box method with style:
GUI.Box(Rect(0, 0, _buttonWidth, buttonHeight),"", _myBoxStyle);

You should edit your style in the skin file the way that it’s normal background contains the image.

  1. Use GUI.Box method with content (GUIContent):
 GUI.Box(Rect(0, 0, buttonWidth, buttonHeight), GUIContent(_myTexture));

_myTexture references your texture.

Thanks, did it

GUI.Box(new Rect(10, 10, 10, 10), GUIContent(_myTexture));

and got UnityEngine.GUIContent’ is a ‘type’ but is used like a ‘variable’ (CS0118) (Assembly-CSharp)

GUI.Box(new Rect(10, 10, 10, 10), new GUIContent(_myTexture));

My mistake, thought we’re in Unityscript. Unityscript and C# are different. You should really learn C# essentials :wink:

Thanks again but…I don’t know how to references your texture in c# ;_;

Put your PNG in the Assets/Resources folder, and then use Resources.Load():

private Texture _myTexture;
void Start () {
    myTexture = Resources.Load(yourTextureUrl);
}

Thanks a lot got it in! :smile: