x


Health Bar in Custom Editor/Inspector

I've been learning Editor scripting for the last few hours and kinda have the hang of it. Right now, I'm trying to make healthbar using a [ProgressBar][1]. I have a health and max health variable is exposed in the inspector, and I can't seem to figure out how use ProgressBar in a way to make the bar work properly.

I know that ProgressBar's value has to be between 0 and 1, so I tried some math, but wasn't successful.

ProgressBar ( (((health.intValue + maxHealth.intValue) / 2) /100.0), "health");

The above code snippet works well when the maxHealth value = 100. So I thought, why not change the "100.0" to maxHealth.inValue?

Well, The bar is filled when health and maxHealth is equal, but if I decrease health, it appears that ProgressBar snaps it's own value from 1 right to 0, because the bar becomes completely empty.

more ▼

asked Jul 03 '12 at 07:09 AM

SirVictory gravatar image

SirVictory
1.7k 64 77 104

Um, shouldn't the equation just read health.intValue / maxHealth.intValue? That will give the fraction between 1 to 0 that health is...

Jul 03 '12 at 07:24 AM asafsitner

It seems to snap/round using that formula.

Jul 03 '12 at 07:31 AM SirVictory
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I think you might just be over complicating this XD

First off the first parameter of ProgressBar is the position, so you might want to try that first.

Then for the value just do:

health.intValue/maxHealth.intValue

simple as that, if health is 50 and max is 100

50/100 = 0.5 Correct!

Remember try not to overcomplicate things :)

more ▼

answered Jul 03 '12 at 07:19 AM

diddykonga gravatar image

diddykonga
159 8 12 14

Thanks, I thought I was at first, but it appears that I get the same result. The bar snaps to either empty or full.

Jul 03 '12 at 07:25 AM SirVictory

Can you post your code? cause it shouldnt do that 0-o

Jul 03 '12 at 07:32 AM diddykonga

Updated the post.

Jul 03 '12 at 07:32 AM SirVictory

Mind posting what values you have as your health and max health, when it snaps?

Jul 03 '12 at 07:38 AM diddykonga

Oh lol just remembered basics of C, you cant divide two ints and get a float value, so you need to make those two floats :)

Jul 03 '12 at 07:45 AM diddykonga
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1668
x465
x94
x29
x6

asked: Jul 03 '12 at 07:09 AM

Seen: 397 times

Last Updated: Jul 03 '12 at 10:28 AM