Space Shooter tutorial 'score' problem

I am following the Space Shooter tutorial as this is my first experience with Unity. For some reason the section involving adding a score to the game isnt working correctly.

I get an error message: ‘NullReferenceException: Object reference not set to an instance of an object.’

This is in relation to this line of code:

gameController.AddScore(scoreValue);

If I disable this line of code the game works but the score doesnt count up, however if this line is enabled the game works but the asteroids in the game will no longer be destroyed, the ships lasers hit them and the sound plays but they carry on falling towards the players ship.

Here is the full script I have (unless I have missed something it should be identical to the example script):

using UnityEngine;
using System.Collections;

public class DestroyByContact : MonoBehaviour
{

  public GameObject explosion;
  public GameObject playerexplosion;
  public int scoreValue;
  private GameController gameController;

  void Start()

  {
  GameObject gameControllerObject = GameObject.FindWithTag("GameController");
  if (gameController != null)
  {
  gameController = gameControllerObject.GetComponent<GameController>();
  }
  if (gameController == null)
  {
  Debug.Log("Cannot Find 'GameController' script");
  }

  }

  void OnTriggerEnter(Collider other)
  {
  if(other.tag == "Boundary")
  {
  return;
  }
  Instantiate(explosion, transform.position, transform.rotation);
  if (other.tag == "Player")
  {
  Instantiate(playerexplosion, other.transform.position, other.transform.rotation);
  }
  gameController.AddScore(scoreValue);
  Destroy(other.gameObject);
  Destroy(gameObject);
  }
}

replace this:

GameObject gameControllerObject = GameObject.FindWithTag("GameController");
  if (gameController != null)
  {
      gameController = gameControllerObject.GetComponent<GameController>();
  }
  if (gameController == null)
  {
      Debug.Log("Cannot Find 'GameController' script");
  }

with this:

gameController = GameObject.FindObjectOfType<GameController>();
if(gameController == null){
    Debug.Log("Cannot Find 'GameController' script");
}

And see if anything changes.

1 Like

Beautiful, that has fixed the problem, thanks! I just wish I understood why the original version didn’t work? Perhaps due to the older version of Unity used in the tutorial?

No it was a logic problem. Lets run through your original script line by line

//finding gameController Object using Tag
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
//if the gameController variable isn't null...
if (gameController != null) {
    //define the gameController variable.
    //But how can the gameController variable possible not be null before you define it?
    gameController = gameControllerObject.GetComponent<GameController>();
}
//if the gameController variable is null...
if (gameController == null) {
    //print a message indicating the GameController script can't be found
    Debug.Log("Cannot Find 'GameController' script");
}

My solution uses GameObject.FindObjectOfType(), which bypasses the need to find the game object first if you are looking for a specific component within a game object in the scene, and the “if” statement only tells you if the gameController component can’t be found if it actually couldn’t be found
If i were to re-write the script per your intention, it would probably be this

GameObject gameControllerObject = GameObject.FindWithTag("GameController");
gameController = gameControllerObject.GetComponent<GameController>();

if (gameController == null) {
    Debug.Log("Cannot Find 'GameController' script");
}

And since those first 2 lines can be supplemented by GameObject.FindObjectOfType(), it becomes the solution I passed you in my previous reply.

1 Like