Null Reference Error For No Reason

I am referencing a class in a script and I can see that it is not null in the editor. I set it in the awake start or just before the method nothing works. As you see in the picture it gives a null reference error for a line that is working. “1” log is called if the reference is not null.

There’s ALWAYS a reason and that reason is that something was NULL!

Fortunately for you, it doesn’t even matter what you are trying to do.

The answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

This is the kind of mindset and thinking process you need to bring to this problem:

Step by step, break it down, find the problem.

Here is a clean analogy of the actual underlying problem of a null reference exception:

I know how to fix the error. But you can clearly see in the picture the line that gives the error is actually working. How can a “Debug.Log()” line that uses the reference work if the reference is null?

Edit:Fixed it. It was not a script problem. It was my brain.

1 Like

Im having the same issue… the script is fine. care to share what happened in your brain??

The same answer applies. It really doesn’t change.

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

This is how to make the brain work:

https://discussions.unity.com/t/814091/4

One needs to learn to trust a) the compiler and b) the runtime engine for basic operations. If we think it is mistaken about null references what do we trust it with? It wouldn’t tell us it was null if it wasn’t null, it has nothing to gain by lying to us.

Check the reference to see if it is null “before” you try to use it, it will turn out to be null.

As a sidenote if someone posts “I’m adding 2 variable values together and it isn’t adding correctly, is this a bug?” the first thought should be that the variables do not contain the values you think they have. It just isn’t possible that Unity or .Net decided to perform math incorrectly today.

1 Like

I just ran into this same scenario and maneged to figurured out what the problem was.
I’m going to describe what happend in case someone is facing the same problem.

In the start() function of one of my classes i was referring to an object that was created in the start() of another class!
In this case, the code would or not work depending on which order unity decided to run the start of those classes(It’s pretty much random in this case)
putting the reference and the creation of the referee under the same “start()” solved it for me, but there are other solutions.
For this you might want to take a look at Execution Order of Event Functions

ps: To all the Techgods in this thread: Yes, there is always a “reason” but it’s not always a CLEAR reason. If you trust the compiler, why can’t you trust that the person asking the question is not a dumdum and HAVE tried the classic solutions already? this is THE most common problem ppl run into, i’m sure that if someone is on a forum asking about it, they know that something strange is going on.

Good information and you determined the error and fixed it. It isn’t about being dumb but about lack of experience in some cases.

My recommendation in most cases is to instrument things until you can’t type Debug.Log any longer. I log every Start method (and every other method) along with passed values and whether references are null. It helps assure me that my assumptions are valid. It is a simple matter to turn off the logging when it is no longer needed.

1 Like

Because this post nearly four years ago, the one starting this thread out:

leaves me with a distinct impression that the person has NOT yet tried the classic solutions.

If they had they would have said “Variable XYZ is null and it should not be because I set it here and proved that this line of code is executing.”

That is why they get the generic “Three step solution.”

NOTE: you’re the only person saying the word “dumdum” in this thread. I have NEVER called someone a dumdum, I have simply offered them the correct solution to ultra-common workaday problems.

I mean neither person posted any code or anything that would otherwise help us point out the problem. How are we meant to give anything other than a general response with a complete lack of information?

1 Like

What do you do when Unity doesn’t even give you any hint or clue what’s null? No script reference. No game object reference. Just a bunch of useless console messages? I have checked every single inspector field, nothing is null. I have checked the only two scripts I have. Nothing is null. And yes, I have checked during play mode as well, indicating nothing is being removed during Awake or Start. And what’s weird is, the game will work, then I will drag something into an inspector field, it will break. Drag the same thing back to the inspector, it works. At no point is anything ever null. I was advised it could be due to reloading domain, which is disabled, as advised.

I mean, like I said in January, not posting the full error, and not posting any relevant code makes it very hard to offer any help.

Sorry.

ArgumentNullException: Value cannot be null.
Parameter name: _unity_self
UnityEditor.SerializedObject.FindProperty (System.String propertyPath) (at <4911eca47f294e18a7b3306f02701303>:0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindPropertyRelative (UnityEngine.UIElements.IBindable field, UnityEditor.SerializedProperty parentProperty) (at <ddc34215cb194bc6b42a1ae3b66800fe>:0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindTree (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at <ddc34215cb194bc6b42a1ae3b66800fe>:0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.ContinueBinding (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at <ddc34215cb194bc6b42a1ae3b66800fe>:0)
UnityEditor.UIElements.Bindings.DefaultSerializedObjectBindingImplementation+BindingRequest.Bind (UnityEngine.UIElements.VisualElement element) (at <ddc34215cb194bc6b42a1ae3b66800fe>:0)
UnityEngine.UIElements.VisualTreeBindingsUpdater.Update () (at <73c3b9fa4da644c9a21a8a16d8e2909f>:0)
UnityEngine.UIElements.VisualTreeUpdater.UpdateVisualTreePhase (UnityEngine.UIElements.VisualTreeUpdatePhase phase) (at <73c3b9fa4da644c9a21a8a16d8e2909f>:0)
UnityEngine.UIElements.Panel.UpdateBindings () (at <73c3b9fa4da644c9a21a8a16d8e2909f>:0)
UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.UpdateSchedulers () (at <73c3b9fa4da644c9a21a8a16d8e2909f>:0)
UnityEngine.UIElements.UIEventRegistration.UpdateSchedulers () (at <73c3b9fa4da644c9a21a8a16d8e2909f>:0)
UnityEditor.RetainedMode.UpdateSchedulers () (at <ddc34215cb194bc6b42a1ae3b66800fe>:0)

I mean from the error its pretty obvious that it’s coming from Unity’s side of things, and nothing to do with your code. Ergo: an editor bug.

I’m assuming you may be on an older LTS version, as I’m pretty sure this bug was fixed in more recent Unity editor versions.

I’m using 2022.3.0, which I believe is the newest LTS version before Unity 6 launched. I hate Unity 6.

The current latest 2022 LTS version is 2022.3.59f1! You’re sitting on the very first update version for the 2022 LTS.

How do you find that version? You have to go to the website? It’s not available in Unity Hub. I would switch versions, but Unity doesn’t know how to handle that.

You can see all versions on the download archive: https://unity.com/releases/editor/archive

And they’re definitely available in the hub. You may be looking in the wrong spot (I’m not on a computer with it installed to take a screen shot).