Getting a NullReferenceException even though the code works

I have the following code:

public Text tapText;
//...
// In the Update method:
  tapText.gameObject.SetActive(!tapText.gameObject.activeInHierarchy);

That line is inside an if statement which basically just checks if a timer has reached a value. So it’s supposed to flash the text by activating and deactivating the game object. The code works and the text flashes in the scene, but I still get a null reference exception in the console each time. Why? I’ve set the tapText in the inspector to a UI.Text element.

Easier to diagnose if you provide the full error message and the lines of code at which it is located. Null references are common and easily diagnosed if the right information is present. Add a null reference check to your script to prevent it from executing that statement if the object is null.

1 Like

They should include this advice in the error message. So many posts are about null references.

I agree with that sentiment. It’s standard practice for a seasoned developer to just add null reference checks without thinking about it, but it’s not something a less experienced developer would tend to do.

Developer Tip of the Day: Use null reference checks; especially if you get a null reference error :slight_smile:

1 Like

I’ve been a .Net developer since .Net first came out so I’m fully aware what a null reference is. I just shouldn’t get one since I have a reference to the UI.Text object and the code works it just still throws a null reference exception.

Understand that I mean no disrespect by this, but if you are getting a null reference error it is because you have a null reference; agreed? Saying that you shouldn’t get one does not change the fact that you have one and for someone who claims to have 13 years in software development that seems to be a pretty ignorant assumption.

If you want more help then you need to post the text from the error in the debug stack and the source from the file. This allows the community to analyze your code more thoroughly and provide you with better resolutions. If you come on here and say you have a null reference error, give one line of code and not give the info from the debug stack, the best anyone can tell you is do a null reference check.

1 Like

In my experience, this is often caused because you have a rogue instance of your behaviour that’s throwing the error. Thus, the instance that you’re aware of in your scene runs quite happily, but somewhere is a behaviour that actually does not have a UIText reference, and chokes up.

Make sure the object that the UI.text is attached to is enabled.

1 Like

Could check if the object is enabled…that might do it.
Could check for a rogue instance…that could be the cause.
Could also post the source and error from the debug stack, have the community analyze that information, and solve the problem in two minutes.

Anything we say is going to be conjecture. There’s really no point in throwing advice at this thread until we get more information. I’m open to helping out, but there’s really nothing anyone can do besides throw out suggestions until something sticks and that just isn’t very productive