- Home /
GUI Texture
I am trying to create a health bar for an enemy which follows the enemy on the screen above its head. Everything works fine, however I have a problem with the GUI Texture component when I try to change the width via java. Here is the code:
var HealthPercentage:float;
HealthPercentage = Health/StartingHealth;
if(transform.FindChild("HealthBar"))
{
var barStart:float;
var barWidth:float;
barStart = transform.FindChild("1x1Border").GetComponent(GUITexture).guiTexture.pixelInset.width;
barWidth = barStart*HealthPercentage;
}
I'm not sure but I think your barStart should be a original width not your current texture width. And if you call the code in loop (every frame) the bar will have a size of 0 make it disappear. I think
Thanks for the reply. What my idea was when doing this was to have two separate GUI textures. I have one for the border (1x1Border) and one for the actual health bar which has a different name. This way I can keep the border the same size and just change the bar width. Because the border is the same size as the bar, it acts as the original width of the bar. I just need to know how to access the width component of the GUI Texture.
Answer by gheeler · Jun 17, 2013 at 10:58 AM
what you can do is draw the background, then use begingroup to limit whats viewable of the next texture
GUI.DrawTexture(//your background
GUI.BeginGroup(new Rect(0, 0, width to use, height))
GUI.DrawTexture(new Rect(0,0,originakWidth,height), //your health bar
GUI.EndGroup();
I have thought about using a GUI texture script but it causes problems because I also have another script attached to the GUI texture game object which allows the health bar to stay above the enemy's head. I couldn't use a script for GUI texture because I can't get it to follow the enemy's head. Thanks though
Your answer
Follow this Question
Related Questions
Fading GUI text 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Setting Scroll View Width GUILayout 1 Answer
How to read note by pressing "E" using RayCast only? 1 Answer
Gui Skin not showing 0 Answers