- Home /
Health Script not working
I have this health thing that theres a icon and text, if the health is over 50% its a certain picture and below 50% is another picture and max and health also have their own pictures. But if it goes down less than 50% the texture changes and if you go back up to over 50% the texture doesnt change back?? how do I fix this
Heres the code:
var curHealth : int = 100;
var mySkin : GUISkin;
var HealthDead : Texture2D;
var Health1 : Texture2D;
var Health2 : Texture2D;
var HealthMax : Texture2D;
function OnGUI(){
GUI.skin = mySkin;
GUI.Box (Rect (10,10,92,116), curHealth.ToString() + "%");
GUI.DrawTexture(Rect(20,40,72,72),HealthMax);
}
function Update () {
if(Input.GetKeyDown(KeyCode.DownArrow)) {
curHealth -= 10;
}
if(Input.GetKeyDown(KeyCode.UpArrow)) {
curHealth += 10;
}
//Health Icon
if(curHealth < 0) {
curHealth = 0;
}
if(curHealth > 100) {
curHealth = 100;
}
if(curHealth >99) {
HealthMax = HealthMax;
}
if(curHealth <100 && curHealth >50) {
HealthMax = Health2;
}
if(curHealth <50 && curHealth >0) {
HealthMax = Health1;
}
}
try using $$anonymous$$athF.Clamp()
to limit the values between your max & $$anonymous$$. (being a c# practitioner i'm not 100% certain of the unityscript syntax)
this:
if(curHealth >99) {
Health$$anonymous$$ax = Health$$anonymous$$ax;
}
does nothing. just delete it and pretend it never existed.
finally, when setting the texture - if you only have 2 total, then check for the conditions for one of them and use else
to set the other. in this case, if your cutoff is 50 then test whether it's greater or equal only - no need for 2 test conditions. something like:
if (curHealth >= 50)
{
Health$$anonymous$$ax = Health1;
}
else
{
Health$$anonymous$$ax = Health2;
}
in c# i'd simply do something like:
Health$$anonymous$$ax = (curHealth >= 50) ? Health1 : Health2;
but that's just me ;)
Answer by SuperLegendOfStarPrime3 · Sep 17, 2014 at 07:10 PM
You're changing HealthMax into all of the other different health images...
The solution is to use a new variable called HealthCurrent instead of applying it all onto HealthMax...
Here's a working version of the code:
var curHealth : int = 100;
var mySkin : GUISkin;
var HealthDead : Texture2D;
var Health1 : Texture2D;
var Health2 : Texture2D;
var HealthMax : Texture2D;
var HealthCurrent: Texture2D = HealthMax;
function OnGUI(){
GUI.skin = mySkin;
GUI.Box (Rect (10,10,92,116), curHealth.ToString() + "%");
GUI.DrawTexture(Rect(20,40,72,72),HealthCurrent);
}
function Update () {
if(Input.GetKeyDown(KeyCode.DownArrow)) {
curHealth -= 10;
}
if(Input.GetKeyDown(KeyCode.UpArrow)) {
curHealth += 10;
}
//Health Icon
if(curHealth < 0) {
curHealth = 0;
}
if(curHealth > 100) {
curHealth = 100;
}
if(curHealth >99) {
HealthCurrent = HealthMax;
}
if(curHealth <100 && curHealth >50) {
HealthCurrent = Health2;
}
if(curHealth <50 && curHealth >0) {
HealthCurrent = Health1;
}
}
Hopefully that works. ;)
Your answer
Follow this Question
Related Questions
How Do We Script a GameObject as a Child? 2 Answers
Object showing different when Build into Mobile 0 Answers
Press "E" to activate Object. Display Gui-texture or Text ?? 2 Answers
LoadLevel Health Question. 1 Answer
How to Show / Hide a GUI Texture 1 Answer