- Home /
How to make a texture cover an entire GUI box
So I have followed some tutorials on YouTube, and I've managed to get a hunger, thirst and health bar working. Everything is absolutely perfect except the textures I put in for the buttons refuse to cover the entire box, even though the images are of the exact same size.
Let me give you an example:
As you can see the textures (which are .PNG files of the correct resoluton [150:20] that are solid colors) What is causing this, and how can I make them cover absolutely all of the dark-grey area?
Thank you in advance, and sorry if I've missed something obvious and is bothering you.
Here is the script btw:
pragma strict
//Size of Textures
var size : Vector2 = new Vector2(150, 20);
//Health Variables var healthPos : Vector2 = new Vector2(20, 20); var healthBarDisplay : float = 1; var healthBarEmpty : Texture2D; var healthBarFull : Texture2D;
//Hunger Variables var hungerPos : Vector2 = new Vector2(20, 45); var hungerBarDisplay : float = 1; var hungerBarEmpty : Texture2D; var hungerBarFull : Texture2D;
//Thirst Variables var thirstPos : Vector2 = new Vector2(20, 70); var thirstBarDisplay : float = 1; var thirstBarEmpty : Texture2D; var thirstBarFull : Texture2D;
//Fall Rate var healthFallRate : int = 150; var hungerFallRate : int = 150; var thirstFallRate : int = 100;
private var ch$$anonymous$$otor : Character$$anonymous$$otor; private var controller : CharacterController;
var canJump : boolean = false;
var jumpTimer : float = 0.7;
function Start() { ch$$anonymous$$otor = GetComponent(Character$$anonymous$$otor); controller = GetComponent(CharacterController); }
function OnGUI() { //Health GUI GUI.BeginGroup(new Rect (healthPos.x, healthPos.y, size.x, size.y)); GUI.Box(Rect(0, 0, size.x, size.y), healthBarEmpty);
GUI.BeginGroup(new Rect (0, 0, size.x * healthBarDisplay, size.y));
GUI.Box(Rect(0, 0, size.x, size.y), healthBarFull);
GUI.EndGroup();
GUI.EndGroup();
//Hunger GUI
GUI.BeginGroup(new Rect (hungerPos.x, hungerPos.y, size.x, size.y));
GUI.Box(Rect(0, 0, size.x, size.y), hungerBarEmpty);
GUI.BeginGroup(new Rect (0, 0, size.x * hungerBarDisplay, size.y));
GUI.Box(Rect(0, 0, size.x, size.y), hungerBarFull);
GUI.EndGroup();
GUI.EndGroup();
//Thirst GUI
GUI.BeginGroup(new Rect (thirstPos.x, thirstPos.y, size.x, size.y));
GUI.Box(Rect(0, 0, size.x, size.y), thirstBarEmpty);
GUI.BeginGroup(new Rect (0, 0, size.x * thirstBarDisplay, size.y));
GUI.Box(Rect(0, 0, size.x, size.y), thirstBarFull);
GUI.EndGroup();
GUI.EndGroup();
}
function Update() { //HEALTH CONTROL SECTION if(hungerBarDisplay <= 0 && (thirstBarDisplay <= 0)) { healthBarDisplay -= Time.deltaTime / healthFallRate * 2; }
else
{
if(hungerBarDisplay <= 0 || thirstBarDisplay <= 0)
{
healthBarDisplay -= Time.deltaTime / healthFallRate;
}
}
if(healthBarDisplay <= 0)
{
CharacterDeath();
}
//HUNGER CONTROL SECTION
if(hungerBarDisplay >= 0)
{
hungerBarDisplay -= Time.deltaTime / hungerFallRate;
}
if(hungerBarDisplay <= 0)
{
hungerBarDisplay = 0;
}
if(hungerBarDisplay >= 1)
{
hungerBarDisplay = 1;
}
//THIRST CONTROL SECTION
if(thirstBarDisplay >= 0)
{
thirstBarDisplay -= Time.deltaTime / thirstFallRate;
}
if(thirstBarDisplay <= 0)
{
thirstBarDisplay = 0;
}
if(thirstBarDisplay >= 1)
{
thirstBarDisplay = 1;
}
}
function CharacterDeath() { Application.LoadLevel("SI$$anonymous$$PLELEVEL"); }
Have you considered using Unity's uGUI ins$$anonymous$$d of legacy? I know some people prefer the scripted/legacy GUI, but honestly, this would be a breeze with uGUI.
In the mean time, I'll take a look at it and see if I can find anything obvious. Would you reformat the code? (Just highlight it all in your post and click the 101010 button again).
I think I've found the issue.
Adjust the "Size" variable in your inspector to 150 and 20.
I used your script in a new scene, and it works as expected (the texture fills the entire box).
Perhaps you accidentally altered the inspector value?
The size is set to 150 and 20 in the inspector. And the pictures I am using are 150x and 20y. No idea how to get this to work. :/
Oh, and I cannot reformat the code, if I try the "comment" button doesn't work. No kidding. >.<
Edit: Thanks for the help though, I am really appreciating it!
Sure thing!
Can you try making some new health bar images and making sure the pixels are 150/20? As I said, it works fine for me, so it's either something with your inspector values or your images. Since we've eli$$anonymous$$ated the inspector issue, that's the only other thing that could be happening from what I can see.