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);
}
}
}
}




