Hello everyone,
As many other users, I am a first timer to both Unity and Programming, so please be patient with any ignorant questions that may come from me.
The Problem
- I keep getting an error that says “NullReferenceException: Object reference not set to an instance of an object” whenever I run the game.
- It is causing the game to detect collision, but does not destroy the objects or update the score.
- There is a particular line of code that is guilty of the error, but I cannot find a clear answer, even with my wonderful Google-fu fingers. The minute I comment-out this offending line, everything works fine.
Details:
- I am following a tutorial for the Space Shooter, and have landed on Step 14 : Counting Points
-
I have gone over the tutorial 3 times, and for the life of me I cannot see what I am doing that is drastically different from the teacher.
-
Please keep in mind that some of the concepts taught in this tutorial were from an older version of Unity. I currently run 5.5.2f1
-
Essentially the script “DestroyByContact” code is attempting to grab a function from one game object (in this case from the game object and class GameController) and use it once an object is destroyed. I thought that I was referencing them accurately, but I guess I am wrong? (Or Unity has changed so drastically that this no longer applies?).
I removed pieces of the code that have nothing to do with the problem at hand for the “GameController” Script. Fyi.
HELP! c:
Debug Log
NullReferenceException: Object reference not set to an instance of an object
GameController.UpdateScore () (at Assets/Scripts/GameController.cs:81)
GameController.AddScore (Int32 scoreValue) (at Assets/Scripts/GameController.cs:76)
DestroyByContact.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/DestroyByContact.cs:40)
DestroyByContact Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyByContact : MonoBehaviour
{
public GameObject explosion; // Reference to grab a public gameObject for explosion- to then instantiate
public GameObject playerExplosion; // Reference to grab a public gameObject for a Player explosion- to then instantiate
public int scoreValue;
private GameController gameController; //GameController is the class name we are referencing to. "gameController" is what we named the variable.
private GameObject gameControllerObject;
void Start ()
{
gameControllerObject = GameObject.FindWithTag ("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent <GameController>();
}
if (gameController == null)
{
Debug.Log ("Cannot find 'GameController' script");
}
}
void OnTriggerEnter(Collider other) // A built in Unity method to handle collision triggers.
{
if (other.tag == "Boundary") // OnTriggerEnter, if tag is boundary, return
{
return; //hands back control to the method that called it. This way, "Boundary" is never destroyed, and is persistently being ignored.
}
Instantiate (explosion, transform.position, transform.rotation); // instatiates explosion OnTriggerEnter
if (other.tag == "Player") // if tag is player, instatiate
{
Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
}
gameController.AddScore (scoreValue);
Destroy (gameObject); // destroys the shot on contact
Destroy (other.gameObject); // destroys an object the laser collides with on contact
}
}
GameController Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
public GameObject hazard; // Public reference for a hazard GameObject, in this case the asteroid
public Vector3 spawnValues; // Vector3 to set the Spawn parameters of the hazard/enemies
public int hazardCount; // Public reference to set how many hazards you want per wave
public float spawnWait; // This variable sets how much time to wait before the next gameObject spawn
public float startWait; // This variable sets how much time before the first spawn starts
public float waveWait; // This variable sets how much time to wait before the next Wave of spawns
public Text scoreText;
private int score;
void Start()
{
StartCoroutine(SpawnWaves ());
score = 0;
UpdateScore ();
scoreText = GetComponent<Text> ();
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore()
{
scoreText.text = "Score: " + score;
}
}