How to track if player is alive using if statements

Hey any help would be greatly appreciated been stuck on this for a while now. I basically want to find a way to track if the player is alive using boolean within the health manager script.

something like

public bool playerAlive;

if(currentHealth > 0)
{
playerAlive == true;
}else(currentHealth<= 0 )
{ playerAlive== false;
}
}

This doesn’t seem to work as it the console says I can’t use currentHealth as a statement.

I have the Health manager script attached.

You cant specify a statement in an else bracket. It doesnt even have brackets. You would either do

else if(currentHealth <= 0)
{
    //Code here
}

or just say

else
{
    //Code here
}

this last one will execute if the if statement above it returned false.
Secondly, remove one of the equals signs, you are doing comparisons, not assignments.

‘else’ doesn’t take arguments.
Btw. you can rewrite the public bool playerAlive field and all this logic to just a single Property:
public bool PlayerAlive => currentHealth > 0; //easy as that

1 Like

Please edit post and use code tags.
It helps read the code nicely.
** Using code tags properly **

public bool playerAlive => currentHealth <= 0;

if (player.GetType() == typeof(Zombie)) […] :slight_smile:

public bool IsAlive (Player p)
{
    if(p.currentHealth <= 0)
    {
        return true;
    }
    return false;
}

Then you can do a check:

if (!IsAlive)
{
    //Death stuff here
}
if(player is Zombie zombie) {
   // a-a-a-a-a--a-a-a-a-aa- zombie!!!!
}

how to in java?

it is the same as c sharp