NullReferenceException
Hello.
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;
void start ()
{
GameObject gameControllerobject = GameObject.FindWithTag ("Gamecontroller");
if (gameControllerobject != 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);
}
}
Is it also printing to the console "Cannot find game controller script"?
Answer by saschandroid · Oct 20, 2015 at 05:57 AM
I guess it has to be
void Start ()
(with a capital 'S') and not
void start()
otherwise the start function is not called at start and you game controller variables are not assigned.
@saschandroid you saved me man, i was this close to give up and none of the other solutions were working for me, but your answer was spot on, thanks
Answer by dsada · Aug 14, 2014 at 02:27 PM
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".
Answer by MrSteve1 · Aug 14, 2014 at 02:26 PM
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.
Hope this helps.
Its the spelling if your doing U5.
if (other.tag != "Player") // not if (other.tag == "Player"){//
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
gameController.GameOver();
}
gameController.AddScore (scoreValue);
Destroy (other.gameObject);
Destroy (gameObject);
}
}
in short you change only one things, the extra = for a !.
Answer by Orc_Horn_Productions · Oct 20, 2015 at 10:13 AM
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.
Let us know how you get on.
Answer by $$anonymous$$ · Oct 20, 2015 at 07:14 PM
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.