-1
\$\begingroup\$

I have created a button as my health bar and it must decreasing after a specified time.Here is the code I have written.But the problem,this code is working in the form "Its decreasing the button bar,but when it decreases I mean when the healthbar=0,the bar is extending to the left most side.I need to stop it when the healthbar=0.Can anybody found solution for this please?

var healthbar=500;
var bar;

function Start () {    
    Invoke("decreasebar",1);
}

function decreasebar(){    
    if(healthbar!=0){
        healthbar-=30;
        Start();
    }
}

function OnGUI(){
    bar=GUI.Button(new Rect(200,200,healthbar,10),"");
}
\$\endgroup\$
6
  • \$\begingroup\$ I have formatted your code but some things seems to be wrong. Specifically, can you check if you are calling start from inside decreasebar? Could you please check that all indenting is organize properly? \$\endgroup\$
    – angarg12
    Commented Aug 5, 2014 at 7:25
  • \$\begingroup\$ its the code working without an error,but the problem in decreasing the progress bar \$\endgroup\$ Commented Aug 5, 2014 at 7:26
  • 1
    \$\begingroup\$ In any case I would advice you to never call the start function from anywhere in the code. It is called at the beginning of the execution for initialization tasks, and it should contain only logic that is supposed to be executed once. \$\endgroup\$
    – angarg12
    Commented Aug 5, 2014 at 7:28
  • \$\begingroup\$ if I wont cal the start() the progress bar get decrease for once and remains idle \$\endgroup\$ Commented Aug 5, 2014 at 7:29
  • \$\begingroup\$ Use InvokeRepeating instead. \$\endgroup\$
    – angarg12
    Commented Aug 5, 2014 at 7:33

2 Answers 2

2
\$\begingroup\$

Your error is on these lines

if(healthbar!=0){
    healthbar-=30;

Because 500 is not divisible by 30, you get something like this:

50, 20, -10, -30...

What you really mean is

if(healthbar>0){
    healthbar-=30;

Because you want to decrease if it is bigger than 0, not if it is different than 0 (very important distinction!).

Also, in order to make the logic complete:

if(healthbar>0){
    healthbar-=30;
}
// Alternatively, put an equivalent check inside the previous if.
if(healthbar<0){
    healthbar=0;
}

That way you make sure that it never goes below 0.

\$\endgroup\$
0
1
\$\begingroup\$

You are only checking to see if healthbar is not zero (if(healthbar!=0)), you should rather check if it is greater than 0, like so:

if(healthbar>0)
{
   //Do something
}

But it might be easier to check if it is less than or equal to zero, so that you can use it to end the game easier, like so:

if(healthbar <= 0)
{
   EndGame();
} else {
   healthbar-=30;
}

Also, this should not be run inside the Start() function, this is very bad practice, you should rather implement this in its own function and call it inside the Start() function in Unity. I have an example you can try, it is in C#, but you can easily use the Unity documentation to change it to javascript, should you be having problems:

public int healthTimer; //Can set in the editor, how often do you want the HP to decrease?
private bool gameOver;

void Start()
{
   ReduceHealth(healthTimer);
}

IEnumerator ReduceHealth(int waitTime)
{
   while(!gameOver)
   {
      yield return new Waitforseconds(waitTime);
      if(healthbar <= 0)
      {
         gameOver = true;
         break;
      } else {
         healthbar-=30;
      }
   }
}

This will start when your game initializes, wait the set amount of seconds before checking if your player is dead, he won't be at first so no problem move on and decrease his HP by 30. It will then wait again, and then check if you are dead, or decrease it again and so on until you do die, where it will end the game and break out of the while loop.

Hope this helps you out.

\$\endgroup\$
3
  • \$\begingroup\$ the above code works fine ...... \$\endgroup\$ Commented Aug 5, 2014 at 8:42
  • \$\begingroup\$ Its accepted.... \$\endgroup\$ Commented Aug 5, 2014 at 10:20
  • \$\begingroup\$ I was unaware of the InvokeRepeating function in unity, that would work nicely as well. I have not edited my answer as I have not used it yet personally. \$\endgroup\$
    – G. Meadows
    Commented Aug 5, 2014 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .