[SOLVED] New here problem with "If" always running

Hello everyone,

I’m very new here, i’m not a coder and never learned how to use C# but i made a few game with Stencyl (easy even without coding knowledge)

I really want to learn how to code properly with Unity or other program using C# so i followed a few tutorial, duplicating and trying to understand how everything work, i decided to try and run a few basic logic with button (to make a RPG)

So basically its just a button trying to reduce the monster life (it kinda … work…) but when i add the line

void Update ()
{
MonsterHP.text = "HP: " + MonsterCurrentHP.ToString();
MonsterHPBar.value = MonsterCurrentHP / MonsterMaxHP;
{
if (MonsterCurrentHP <= 0.1);
{
MonsterCurrentHP = MonsterMaxHP;
}
}

}

The MonsterCurrentHP already stay at the MonsterMaxHP, if i remove this “If” then monster health goes to 0 and even further in the -

Any idea why it doesnt follow the logic to wait for CurrentHP to be under 0.1 ?

Use code tags. It’s an icon on the toolbar where you reply or post.
Here is your code formatted.

void Update()
{
    MonsterHP.text = "HP: " + MonsterCurrentHP.ToString();
    MonsterHPBar.value = MonsterCurrentHP / MonsterMaxHP;
    {
        if (MonsterCurrentHP <= 0.1) ;
        {
            MonsterCurrentHP = MonsterMaxHP;
        }
    }

}

What is going wrong here is that you are not paying attention to errors. When an error happens (red text in console), the game has crashed and might still be running. So double click the error to find it. If you use visual studio, the IDE will have already spotted these errors for you.

OK so what was the problem? Remember that all floating point numbers must have an f on the end to signify that it is a float. It’s one of those rules about C# that makes more sense later.

1.0 = bad
1.0f = good

But only if it’s a float and not an integer or something else :slight_smile:

The other problem is that you put a semi colon to indicate the end of a line to the compiler. ; on a condition. That’s not allowed. You can’t put ; on the end of an if.

if (a == b); = bad
if (a == b) = good

The final problem is you have a couple of curly braces that make no sense at all, because they aren’t part of a condition or block of code like an if statement. { } need to signify the start and end of a block, not any old code.

if (something)
{
//good!
}

a += 10;
{
//er nope!
}

So your working code maybe looks a little like this (I can’t check as you didn’t post everything):

void Update()
{
    MonsterHP.text = "HP: " + MonsterCurrentHP.ToString();
    MonsterHPBar.value = MonsterCurrentHP / MonsterMaxHP;

    if (MonsterCurrentHP <= 0.1f)
    {
        MonsterCurrentHP = MonsterMaxHP;
    }
}

Happy coding and feel free to ask as many questions as you like as often as you like and it’s usually best to make more threads if the problem is a bit different to the last problem you had. You’ll find your feet.

Thank you very much for the reply i understand everything now =)

I used a few number with “f” at the end but forgot this one

For the semi colon i saw and used it a lot (as they use it very often in the tutorial or other video) but until now i didn’t knew it was clearly indicating the end of a line, as the next one would be read appart, i though it was the braces thats why i used … too much of them haha

Thanks for all the precision it’s really not easy to start with C# and Unity from no knowledge at all but i start to get the logic and i’m doing some fun mechanic (i’m sticking to idle… and some small rpg thing it’s easier to start with than going to a fully fonctionnal shooter or other kind of game with moving object and collision)

I redid the code for this part, now everything is working but i just spent like 1h to find out why my .ToString(F1) wasn’t working … and still dont understand why it require to use (“F1”) =(

Google “ToString(F1)” and you should find your answer.

Yes i googled it i mean i understand how it work but not why its (“F1”) and not (F1) even googling i still dont catch why the “” need to be here clearly

It is a precision specifier and has to be passed into the function as a string and not a numeric type. See Standard numeric format strings - .NET | Microsoft Learn

(It’s just how microsoft designed it).

I see it was designed like that that’s what i though anyway its really hard when you never coded before to start haha but i’m doing great at the moment i managed to do the whole fighting system and reward + some skills growing over time with a energy function
I got another… big problem for my game i’m gonna start a new thread, i found many “hint and way to solve it”… sadly i still dont understand and none is exactly like mine so maybe some people around here could give me more informations and a solution maybe