- Home /
GUI healthbar
Hi there. I need help from anyone. Im new in unity i'm doing my healthbar. I'm using texture. Im not sure how am i gonna declare the texture cause i keep getting error unity texture 2d cannot convert to int. all i wanted was my healthbar to change its width as the healthcount is changing too.
here's my script. hope someone can help me guys.
var maxHealth = 100;
var currHealth;
var damage :int = 10;
var myHealthbar:Texture2D ;// green
var healthbarWidth :int;
var myhp;
function Start () {
healthbarWidth = 100;
//var myhp=Instantiate(myHealthbar, transform.position, transform.rotation);
}
function Update()
{
if (Input.GetKeyDown(KeyCode.F8))
ApplyDamage();
}
function ApplyDamage ()
{
maxHealth = maxHealth - damage ;
healthbarWidth = maxHealth;
healthbarWidth = myHealthbar;//maxHealth * 100;
GUI.Box( Rect (10,10, 100,50), myHealthbar);
print(maxHealth);
if (maxHealth == 0)
{
die();
}
}
function die ()
{
yield WaitForSeconds(2.0);
Destroy(gameObject);
}
thanks in advance:)
Ok, where do I start with this. Oh I know- editing your post so that I can actually read it.
You should try reading the documentation before attempting to use functions like GUI.Box. Also, it seems that you are trying to treat your texture2D as if it were an integer- which you obviously can't do. Try myHealthbar.width ins$$anonymous$$d.
Answer by IMoriarty · Dec 06, 2011 at 08:44 AM
Haven't worked much with the GUI yet, but I'd start by looking at updating the Size of the GUI.Box based on the amount of health left as a percentage. So something like:
GUI.Box( Rect (10,10,healthbarWidth,50), myHealthbar);
Answer by chammybui27 · Dec 06, 2011 at 09:53 AM
@ syclamoth actually i wanted to put the value of the healthcount or lives into the GUI healthbar that's why i'm trying to put the healthbarwidth equals to the myhealthbar which is a texture. I don't really how to declare the texture width that when the healthcount is decreasing the texture GUI healthbar as well.hope you get what i meant.
@IMoriaty I'm trying to separate the script from the health system to the healthbar. but i'm still having problem with the texture..
Please, don't post comments as answers. It makes these threads terribly messy.
Answer by DryTear · Jun 06, 2013 at 07:17 PM
on line 24, do this :
HealthBarWidth = myHealthbar.width;
and on line 25:
Rect(x,x,myHealthbar.width,x)
Your answer
Follow this Question
Related Questions
Why the healtbar is moving ? 0 Answers
Adding a floating health bar 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Any problems with this script? 1 Answer