NullReferenceException Error

I’m not sure what this error really is, nor why I’m getting it or how to fix it. It doesn’t seem to prevent me from doing anything, everything in my project works as intended, but as soon as I start the game, I get the error in the console. It just started happening out of seemingly nowhere as well, which is odd.

I thought it was because I had a script needing to point to an object and not having actually set it to point to anything, but I triple checked all my objects and their attached scripts, and that’s not the case.

Am currently using Unity 2022.3.6f1 if that changes anything, this is the log message:

NullReferenceException: Object reference not set to an instance of an object
UnityEditor.U2D.Animation.SpriteLibraryEditor.SpriteLibraryEditorWindow.OnDestroy () (at ./Library/PackageCache/com.unity.2d.animation@9.0.3/Editor/SpriteLib/SpriteLibraryEditor/SpriteLibraryEditorWindow.cs:61)
UnityEditor.EditorApplicationLayout:InitPlaymodeLayout()

Another indication this 2022 thing isn’t really LTS, despite it being claimed as such. :confused:

This specific error, coming from this location (eg, NOT your code, such as UnityEditor.Graphs), is often fixed by either Reset Layouts (upper right) or else Right-click Project, Reimport-All.

Or uninstall the sprite library editor package, then reinstall it.

Otherwise, if the error is in your code, it’s always the same answer:

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

Extra unwanted packages in new projects (collab, testing, rider and other junk):

https://discussions.unity.com/t/846703/2

About the fastest way I have found to make a project and avoid all this noise is to create the project, then as soon as you see the files appear, FORCE-STOP (hard-kill) Unity (with the Activity Manager or Task Manager), then go hand-edit the Packages/manifest.json file as outlined in the above post, then reopen Unity.

Sometimes the package system gets borked from all this unnecessary churn and requires the package cache to be cleared:

https://stackoverflow.com/questions/53145919/unity3d-package-cache-errors/69779122

The other guy wasn’t very helpful. We know what a null pointer exception is… but there’s nothing to search if the null pointer exception isn’t in your code or assets.

If you’re still stuck, for me, this ended up being because the upgrade to Unity 6 added URP to my HDRP project. Removing the URP package in the package manager fixed the issue. I found this by identifying that the code was trying to execute as if the constant ENABLE_URP was defined.