- Home /
NullReferenceException: Object reference not set to an instance of an object
Hello, I am having problems creating a point system for my 2D space shooter game (I am following the unity tutorial) The problem I am having, is that my game stops when I receive the "points" And then shows me this error: NullReferenceException: Object reference not set to an instance of an object DestroyByContact.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/DestroyByContact.cs:30) "
These are my 2 scripts, which should work together:
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(gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent <GameController>();
}
if(gameController = null)
{
Debug.Log ("Cannot find Gamecontroller Script");
}
}
void OnTriggerEnter2D(Collider2D other) {
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);
}
}
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour {
public GameObject hazard;
public Vector2 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public GUIText scoreText;
private int score;
void Start () {
score = 0;
UpdateScore();
StartCoroutine (SpawnWaves ());
}
IEnumerator SpawnWaves () {
yield return new WaitForSeconds (startWait);
while(true){
for (int i = 0; i < hazardCount; i++){
Vector2 spawnPosition = new Vector2(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds(waveWait);
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
void UpdateScore()
{
scoreText.text = "Score: " + score;
}
}
Any help is appreciated, thanks!
Answer by SebastianRSP · Feb 16, 2014 at 04:38 PM
I found the error, and it was just a small typing fault on my side, line 18 if(gameController = null)
Should have 2 "="
So it would look like this instead: if(gameController == null)
Thanks for the answers :)
Answer by NoxCaos · Feb 16, 2014 at 04:14 PM
You cann't just call the function of the GameObject. You should use function SendMessage. Just like this:
gameController.SendMessage("AddScore", scoreValue);
Don't forget to assign your gameController. And you'll have another error in your AddScore function. Change to:
scoreText.text = "Score: " + score.ToString();
Actually, that's incorrect, you can call the function if you have stored the component that contains that function, which is what he did. Using GetComponent in your Start() is much more efficient than calling Send$$anonymous$$essage every time you want to access that. Send$$anonymous$$essage should be used verrrry sparingly.
If you store a script in a variable at Start, you then have access to all it's public functions or variables for as long as the script is alive, and can pass info to those functions as well, without the major performance overhead of Send$$anonymous$$essage or trying to get the component each time.
Your answer
Follow this Question
Related Questions
setTrigger() for another GameObject doesn't work 1 Answer
How to send a gameobject to a sepcific position in the 3d environment? 3 Answers
At same positioned game objects collision detection 0 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Rotate a gameobject(player)on the y axis towards mouse position in c# 2 Answers