"unity object reference not set to an instance of an object"

I know this is asked a lot, but I need help with my script itself.
I am following a tutorial, where you move a person around, collect a coin, and if you get hit by an enemy u start over and need to reach the end goal to get to level 2. It had been working perfectly fine. Without touching my player script, I started to work on the UI, again following the tutorial. I hit play, go through my motions to collect my coin to test the UI, and suddenly I get errors saying that the object reference isn’t set. The items I am interacting with have the tags, everything is named correctly nothing had been touched between my trial run before starting the UI script and after I am done.

// Did we hit the enemy?
void OnTriggerEnter(Collider other)
{
if(other.CompareTag(“Enemy”))
{
GameManager.instance.GameOver();

}
// Did we hit a coin?

else if(other.CompareTag(“Coin”))
{
GameManager.instance.AddScore(1);
Destroy(other.gameObject);
audioSource.Play();
}

// Did we hit goal?

else if(other.CompareTag(“Goal”))
{
GameManager.instance.LevelEnd();
}

If you need to see anything else, please let me know. I am a beginner at this and I just cannot see what changed. Visual Studio itself isn’t flagging that anything is missing.

It says "NullReferenceException: Object reference not set to an instance of an object
PlayerController.OnTriggerEnter (UnityEngine.Collider other)(at Assets/Scripts/PlayerController.cs:80)

and repeats for 73, and 89, which all are Enemy, Coin, Goal.

Use code tags.
Copy and paste the error as it gives line numbers
But, truth be told if I had to guess it is probably your GameManager.Instance might be null. Did you actually set a value for it?

I was so concerned about the script I didn’t look to see if the _GameManager was there, and once it again it got oddly deleted. However, I recreated it and I am still having this problem.

But this is my game manager script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
public int score;

// instance
public static GameManager instance;

private void Awake()
{
if (instance != null && instance != this)
{
Destroy(gameObject);
}
else
{
instance = this;
DontDestroyOnLoad(gameObject);
}
}

public void AddScore (int scoreToGive)
{
score += scoreToGive;
GameUI.instance.UpdateScoreText();
}

public void LevelEnd ()
{
// is this last level?
if (SceneManager.sceneCountInBuildSettings == SceneManager.GetActiveScene().buildIndex + 1)
{
WinGame();
}
else
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}

public void WinGame ()
{
GameUI.instance.SetEndScreen(true);
}

public void GameOver ()
{
GameUI.instance.SetEndScreen(false);
}

}

I am also now getting this:

NullReferenceException: Object reference not set to an instance of an object
GameManager.AddScore (System.Int32 scoreToGive) (at Assets/Scripts/GameManager.cs:29)
PlayerController.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/PlayerController.cs:80)

NullReferenceException: Object reference not set to an instance of an object
GameManager.AddScore (System.Int32 scoreToGive) (at Assets/Scripts/GameManager.cs:29)
PlayerController.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/PlayerController.cs:80)

Look rotation viewing vector is zero
UnityEngine.Transform:set_forward(Vector3)
PlayerController:Move() (at Assets/Scripts/PlayerController.cs:47)
PlayerController:Update() (at Assets/Scripts/PlayerController.cs:23)

NullReferenceException: Object reference not set to an instance of an object
GameManager.AddScore (System.Int32 scoreToGive) (at Assets/Scripts/GameManager.cs:29)
PlayerController.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/PlayerController.cs:80)

Reformat with code tags, I’m not going to count lines to figure out what line the error is actually on.

Otherwise, the error is here GameManager.cs:29 which says the script and line number. And it’s a null error, so something on that line is null.

I could tell you what is most likely null, but let’s see if you can figure that one out :slight_smile:

Not sure why, but my game UI script was removed from my canvas so the game manager had nothing to reference to x.x TY