I follow the tutorial for a space shoter from Unity and i have a problem in the final script for counting the score.After i wrote the script i haven’t any problems in Console,but when i enter in Play Mod and hit the first asteroid the game crush and give me that error: “NullReferenceException: Object reference not set to an instance of an object”.
When i double click on the error the program tell’s me that is a problem with this line: gameController.AddScore (scoreValue);(Sorry for my bad english -.- )
using UnityEngine;
using System.Collections;
public class DestroyByTouch : MonoBehaviour
{
public GameObject explosion;
public GameObject playerExplosion;
public int scoreValue;
private GameController gameController;
It can only be doing this because the game object tagged “Gamecontroller” is not being stored in the variable “gameControllerobject”, possibly tag is spelt differently in scene view, or the object does not have the script “GameController” attached to it.
It has to be one of those two options. Check them out.
Doesn’t it logs out that “Cannot find GameController script” ?
Because the only possible null value is the gameController variable and you log out this message if it is null.
Most probably you dont have an object on the scene that is tagged with “GameController”.
I believe this could be a missleading error. If you’ve followed the tutorial through it could be that you’ve forgotten to set your public Text element from GameController in the inspector.
This would explain the NullReferenceException because when DestroyByTouch calls gameController.AddScore (scoreValue) the GameController has no reference to add the score to the UI through.
Check your GameController in the inspector first and if that fails try using the your compilers debugger with a break point on “gameController.AddScore (scoreValue)”. That will help prvide more information.
OK i give up on space-shooter, I don’t know if others have the same issue or not but I made all the corrections, it was starting to work, then the next time I opened it, boom, over half the scripts locked down game play and gave error after error, line after line. The Engine itself tried to change things and screwed it all to hell and back.