Object Reference is not set to an instance of an object

I’m trying to add a score counter to my game and I’m using the one from the unity roll a ball tutorial since it seemed pretty simple. I’m also using the same compare tag code that the tutorial used to increase the count but not to destroy the other objects since I’m using code from a different tutorial for that already. I ended up putting the score code in a separate script since I’m following two tutorials at the same time so it seemed easier than putting it in the player controller script. I’ve also already added the TextMeshProUGUI to the script in the inspector but when an enemy dies it does not increase the count and just gives me the error mentioned above and I’m not sure why since I did add the TextMeshProUGUI to the code in the inspector. Here is the code I am using:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class Count : MonoBehaviour
{
    private int count;

    public TextMeshProUGUI countText;
   
    // Start is called before the first frame update
    void Start()
    {
        count = 0;
        SetCountText();
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Animal"))
        {
            count = count + 1;
            SetCountText();
        }
    }

    void SetCountText()
    {
        countText.text = "Count: " + count.ToString();
    }
}

And a screenshot of the inspector window is attatched.

In precisely the same exact way that was the answer the LAST time you posted about this on September 1, 2023, guess what? The answer has not changed.

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

Would help to know where the error is occuring (as in post the full error, otherwise we’re shooting in the dark).

I would also use the second overload of Debug.Log that pings the object that fired the error. Of these “I assigned it but there’s still a null-ref” error is because there’s an instance of a component somewhere in a scene that you forgot about, where you haven’t assigned anything to.

The full error is “NullReferenceException: Object reference not set to an instance of an object Count.SetCountText () (at Assets/Scripts/Count.cs:36)”

Instead of

 void SetCountText()
    {
        countText.text = "Count: " + count.ToString();
    }

//try this

void SetCountText()
{
   countText.text =  string.Format("Count: {0}", count);
}

There should be a video about null reference exceptions :slight_smile: You see the line number right? There is only one object on line 36 so surely that is null.

Oh gosh don’t use string.Format. The solution to fixing bugs is not to guess until it goes away but to “reason” the cause, test that theory and then to fix the actual problem.

A simply log line would indicate that reference is null. count isn’t a reference it can’t be null.

Just checked that out to see if it worked, unfortunately the error is the exactly same as before.

I assume you mean the countText object. I dragged the script onto one of my objects and I added the Text Mesh Pro UGUI object to the script in the inspector. I added a screenshot of the inspector window where I did this in my original post.

you need to investigate it strange that it showing you an integer as null reference makes no sense.

try
{
countText.text = string.Format("Count: {0}", count);
}
catch(Exception e)
{
    Debug.Log(e.Message);
}

P.S. I think your issue is related to other script i guess you trying to pass this value in another script

Using a string.Format generate less garbage using zString with format is even better. Just writing as a “normal string” value and declaring ToString will increase it significantly it’s a good purpose at least to learn that. If you trigger every time that enemy is died from OnTriggerEnter make it even worse since it keep regenerate it each time.

Update: I just saw that the error is coming from the fact that the script IS attached to the game object in the scene but NOT to the prefab I’m using to spawn new items whenever the player presses space. This means that the object doesn’t have the Text Mesh Pro UGUI attached to it when new ones spawn. BUT unfortunately things aren’t completely solved. For some reason even though I can attach the text object to the game object in the script, I can’t attach it to the prefab for some reason and I’m not sure why since the game object is a copy of the prefab and the script is the exact same.

Look, you can write as many forum posts as you like but this error WILL NOT BE FIXED by those posts.

First, just ignore ALL the posts about format and garbage collection. That’s COMPLETELY irrelevant to your problem.

The null reference exception will only be fixed by doing the three steps.

The longer you delay starting on step #1 below, the longer you will be stuck on this error.

I’m not kidding. That’s how these things work. I’m a professional software engineer, trust me.

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

In my previous post I just said what was null. To use your formula, here is where I’m at now:

-Identify what is null ← The count text object in the prefab of the game object.
-Identify why it is null ← Because I can’t add the Text Mesh Pro UGUI object to the prefab like with the individual game object
-Fix that ← I don’t know how.

The first attached picture shows that I can and have added the text object to the game object in the scene and the second shows that I can’t add it to the prefab even though it is the same object and the same script.

9453266--1327343--Screenshot (3).jpg
9453266--1327346--Screenshot (2).jpg

You can’t add scene references to prefabs. You should assign the text object when you spawn the new item.

1 Like

Now we’re getting somewhere!

From your pictures above:

Food Pizza looks like a prefab.

A prefab lies on disk until it is instantiated.

You have dragged in one food pizza copy into your scene.

The CountText looks like it is simply in your scene as a Canvas UI hierarchy.

And this is the important part:

A prefab on disk could NEVER reference ANYTHING live in a scene.

You CAN drag that text object into the in-scene instance of the prefab, but it could NEVER be saved into the prefab and would ALWAYS be a single overload on this one single pizza lying in your scene.

REMOVE the pizza from your scene to REMOVE this temptation.

Make your pizza spawner either inject the necessary data into the pizza objects when it spawns or else…

The typical construct in Unity is to make a GameManager or perhaps a ScoreManager that is in charge of tallying score.

Your food pizza would locate that object (there are many ways, read below) and add to the score.

ULTRA-simple static solution to a GameManager:

OR for a more-complex “lives as a MonoBehaviour or ScriptableObject” solution…

Simple Singleton (UnitySingleton):

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

These are pure-code solutions, DO NOT put anything into any scene, just access it via .Instance

Alternately you could start one up with a RuntimeInitializeOnLoad attribute.

The above solutions can be modified to additively load a scene instead, BUT scenes do not load until end of frame, which means your static factory cannot return the instance that will be in the to-be-loaded scene. This is a minor limitation that is simple to work around.

If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}

There are also lots of Youtube tutorials on the concepts involved in making a suitable GameManager, which obviously depends a lot on what your game might need.

OR just make a custom ScriptableObject that has the shared fields you want for the duration of many scenes, and drag references to that one ScriptableObject instance into everything that needs it. It scales up to a certain point.

And finally there’s always just a simple “static locator” pattern you can use on MonoBehaviour-derived classes, just to give global access to them during their lifecycle.

WARNING: this does NOT control their uniqueness.

WARNING: this does NOT control their lifecycle.

public static MyClass Instance { get; private set; }

void OnEnable()
{
  Instance = this;
}
void OnDisable()
{
  Instance = null;     // keep everybody honest when we're not around
}

Anyone can get at it via MyClass.Instance., but only while it exists.