Null Reference Exception
Hi! I am just making a simple 2D vertical shooter. I am trying to get the text "Game Over" to come up when an enemy collides with the player. However, on the collision script it is giving me a Null Reference Error: Object reference not set to an instance of an object. From what I can see it all looks like it should be fine so I am not sure what exactly is wrong? Any help is appreciated. Thanks very much!!
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.tag == ("Player")) {
gameObject.SetActive (false);
gameOverText.GetComponent<Text> ().enabled = true;
}
Whenever you are posting about an error, you should post the row where the error points to.
Basically anything null on the left side of a dot will cause a null reference error, so in your case the possibilities are other
, other.gameObject
, gameOverText
or gameOverText.GetComponent<Text> ()
. Debug.Log them out in that order and you'll see which. Then you can figure out why