"If" statement gets called before the condition is 100 (when it's supposed to be called in my case)

Hey everyone! Can somebody explain me how does it get past the “if” statement if the currentHealth isn’t 100 yet so it just kind of ignores the “if” statement (which will prevent the health to go more than 100) and sets itself to 100
just jumps for 60 to 100.

This does not happen if the i put nothing in that “if” statement than it regenerates normaly but it has no cap.
So i hope you get the idea.

Heres my code:

void Update ()
    {
        timer -= Time.deltaTime;
        rend.material.SetFloat ("_Scale", Mathf.Lerp (rend.material.GetFloat("_Scale"), 0f, Time.deltaTime * 3));
        Debug.Log (currentHealth);
         if (timer <= 0)
        {
            currentHealth = currentHealth + Time.deltaTime * 8f;
            if (currentHealth >= 100f);
            {
                currentHealth = 100f;
            }
        }
    }

Have you tried removing the semi colon on your if statement at line 9? The semicolon always stops a line of code so if you want the code within your if statement to run the semicolon is blocking it so that might well be why it’s acting so weird.

1 Like

Debug.Log should be after if statement, so it should look like this:

void Update ()
    {
        timer -= Time.deltaTime;
        rend.material.SetFloat ("_Scale", Mathf.Lerp (rend.material.GetFloat("_Scale"), 0f, Time.deltaTime * 3));
         if (timer <= 0)
        {
            currentHealth = currentHealth + Time.deltaTime * 8f;
            if (currentHealth >= 100f)
            {
                currentHealth = 100f;
            }
            Debug.Log (currentHealth);
        }
    }
1 Like

omg Thanks Lethn

You know i used to forget about semicolums but now im overusing them :smile:D

LOL! No problem glad it was a simple fix, capitalisations are also a constant annoyance you need to keep an eye out for or, currently, I’m dealing with code placement problems a lot.

Be sure to change your debug code like K1ng Zero said so you know everything is working too.