[HELP] Track the Lifepoints of the Playership don´t work!

Hello everyone,

I just want to track the Lifepoints of the Playership and had the idea to simply reconfigure the ScoreKeeper-Skript into a new Skript called LikeKeeper witch basically does the same thing as the Score-Skript (tracking the impact of the Enemy-Lasers and showing the decrease of Health of the Playership).

ScoreKeeper.cs

void Start()
{
    myText = GetComponent<Text>();
    Reset();
}

public void Score(int points)
{
    Debug.Log("Scored Points");
    score += points;
    myText.text = score.ToString();
}

public void Reset()
{
    score = 0;
    myText.text = score.ToString();
}

EnemyFormation.cs

private ScoreKeeper scoreKeeper;

void Start()
{
    scoreKeeper = GameObject.Find("Score").GetComponent<ScoreKeeper>();
}

void OnTriggerEnter2D(Collider2D collider)
{
    Projectile missile = collider.gameObject.GetComponent<Projectile>();
    if (missile)
    {
        health -= missile.GetDamage();
        missile.Hit();
        if (health <= 0)
        {
            Die();
        }
    }
}
void Die()
{
    AudioSource.PlayClipAtPoint(deathSound, transform.position);
    scoreKeeper.Score(scoreValue);
    Destroy(gameObject);
}

LifeKeeper.cs

void Start()
{
    myText = GetComponent<Text>();
    ResetLife();
}

public void Life(int points)
{
    Debug.Log("Lost Lifepoints");
    life -= points;
    myText.text = life.ToString();
}

public void ResetLife()
{
    life = 200;
    myText.text = life.ToString();
}

PlayerController.cs

public int lifeValue = 50;

private LifeKeeper lifeKeeper;
public AudioClip fireSound;
public AudioClip deathSound;

void OnTriggerEnter2D(Collider2D collider)
{
    Projectile missile = collider.gameObject.GetComponent<Projectile>();
    if (missile)
    {
        health -= missile.GetDamage();
        missile.Hit();
        if (health <= 0)
        {
            Die();
        }
    }
}

void Die()
{
    AudioSource.PlayClipAtPoint(deathSound, transform.position);
    lifeKeeper.Life(lifeValue);
    Destroy(gameObject);
    Application.LoadLevel("Win Screen");
}

This script is meant to show the Player how much Health he has left.

The state of the Situation is that basically everthing works (Enemies and Player are spawning, both fire Projectiles, Sounds of fired lasers, hits and destroyed Enemies are playing and both Score and Lifpoints are shown) but for some reason the Playership won´t die and the Lifecounter won´t decrease.

I hope I was specific enough with the problem and would really appreciate have any advice from you that may help me. I´m sorry if messed up the question a little bit because this my first time I asking for help here.

Do you link the LifeKeeper in the Start Method of the PlayerControler.cs?
When I rebuild your code I get the same error as you. But when I add
lifeKeeper = GameObject.Find("Life").GetComponent<LifeKeeper>();
then it will work.

cheers

Privacy & Terms