Need a Health Bar script for my game.

I’m currently making a space shooter game in which when a collider(asteroids, enemy ship, lasers etc.) hits my spaceship the health value decreases by 1 out of 10. Can anybody help me to make this script, I’m a newbie and my knowledge in C# is not much.

Hi. Assuming you already have colliders set up for the objects, a script like this should work:

public int maxHealth = 10;
private int health;
private bool playerDied;

void Start()
{
    playerDied = false;
    health = maxHealth;
}

private void OnTriggerEnter(Collider obj)
{
    if(col.tag == "debris")
    {
        DecreaseHealth();
    }
}

private void DecreaseHealth()
{
    health = health - 1;

    if(health <= 0)
    {
        playerDied = true;
    }
}

Haven’t tried the script myself but it should work. Just set up a tag for all the objects called ‘debris’ or whatever you want and try it out! Hopefully it helps.