[SOLVED] Score not updating in-game

So two silly errors I’m finding when I run the game and can’t seem to figure out.

  • When in the inspector my public score value is always set to 0.
  • When setting the score to any value and destroying an enemy object the score will update to that new value
    once and will not add an additional score value further, regardless of enemy objects being destroyed.

//ScoreKeeper script

  public void Score(int points) {
    Debug.Log("Enemy ship down!");
    score += points;
    scoreText.text = score.ToString();
  }

The Debug.Log here is returning it’s message in the console when an enemy object is destroyed. So I’d say this block is fine and working right. Although the “score += points;” isn’t doing it’s function properly.


//EnemyBehaviour script

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

void OnTriggerEnter2D(Collider2D collider) {
    
    Projectile missile = collider.gameObject.GetComponent<Projectile>();
    if (missile) {
        Debug.Log("Enemy hit by laser");
        health -= missile.getDamage();
        missile.Hit();
        if (health <= 0) {
            GameObject.Destroy(gameObject);
            scoreKeeper.Score(scoreValue);
        }
    }
}

So I imagine the issue is within one of these blocks. although they look fine to me… So is there somewhere else I should look to that may be causing this issue?

(Screenshots below displaying the in-game error.)


//Score set to 10

//Score reset to 0

// One enemy object destroyed and the bottom right text has changed to 10 after changing the public score to 10.

// Second enemy object destroyed and the bottom right text remains the same, although the console Debug.log registers it’s destruction.

// Changing the score value from 10 - 20 and it will change the score once I’ve destroyed another enemy object.


// ScoreKeeper Script (Full)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

    public class ScoreKeeper : MonoBehaviour {

    public int score;
    private Text scoreText;

    void Start()
    {
        scoreText = GetComponent<Text>();
        reset();
    }

    public void Score(int points) {
        Debug.Log("Enemy ship down!");
        score += points;
        scoreText.text = score.ToString();
    }

    public void reset()
    {
        score = 0;
    }
}

//EnemyBehaviour Script (Full)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyBehaviour : MonoBehaviour {

    public GameObject projectile;
    public float projectileSpeed = 10f;
    public float health = 100;
    public float shotsPerSecond = 0.5f;
    public int scoreValue = 10;
    private ScoreKeeper scoreKeeper;

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

    void Update() {
        float probability = Time.deltaTime * shotsPerSecond;
        if (Random.value < probability){
            Fire();
        }
    }

    void Fire() {
        Vector3 startPosition = transform.position + new Vector3(0, -1, 0);
        GameObject missile = Instantiate(projectile, startPosition, Quaternion.identity) as GameObject;
        missile.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -projectileSpeed);
    }

    void OnTriggerEnter2D(Collider2D collider) {
    
        Projectile missile = collider.gameObject.GetComponent<Projectile>();
        if (missile) {
            Debug.Log("Enemy hit by laser");
            health -= missile.getDamage();
            missile.Hit();
            if (health <= 0) {
                scoreKeeper.Score(scoreValue);
                GameObject.Destroy(gameObject);
            
            }
        }
    }
}

As a quick update I put a Debug.Log within the start function just as precaution.


//EnemyBehaviour script

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

And it returns a message in the console for every enemy object within the scene. So that is also working.
So the only possible place for an issue can be would be the OnTriggerEnter2D block… I think. Again it looks fine to me so I imagine I’m actually looking in the wrong direction.


//EnemyBehaviour script

void Start() {
scoreKeeper = GameObject.Find(“Score”).GetComponent();
}

void OnTriggerEnter2D(Collider2D collider) {

    Projectile missile = collider.gameObject.GetComponent<Projectile>();
    if (missile) {
        Debug.Log("Enemy hit by laser");
        health -= missile.getDamage();
        missile.Hit();
        if (health <= 0) {
             GameObject.Destroy(gameObject);
             scoreKeeper.Score(scoreValue);
      }
   }
}

I’ve solve my issue. Even just writing it all out here helped clear my head with what I needed.
Here are the changes in case someone is interested or came across a similar issue.


In the ScoreKeeper script I included a " public int points = 10; " so I can control the point increment within the inspector. Then I changed the Score(); event and not have it call " int points " as I would just call for the increases in score through my new " public int points ".

    public int points = 10;
    public static int score = 0;
    private Text scoreText;

    void Start()
    {
        scoreText = GetComponent<Text>();
        reset();
    }

    public void Score() {
        Debug.Log("Enemy ship down!");
        score += points;
        scoreText.text = score.ToString();
    }

    public void reset()
    {
        score = 0;    
    }
}

Then within the EnemyBehaviour script under OnTriggerEnter2D event I changed how I the scoreKeeper would be called simply by calling the scoreKeeper.Score(); script without any argument within.

void OnTriggerEnter2D(Collider2D collider) {

    Projectile missile = collider.gameObject.GetComponent<Projectile>();
    if (missile) {
        Debug.Log("Enemy hit by laser");
        health -= missile.getDamage();
        missile.Hit();
        if (health <= 0) {               
            GameObject.Destroy(gameObject);
            scoreKeeper.Score();
        }
    }
}

Now my score is increasing with every enemy object destroyed and additionally I can change the score increments within the game for now.