Script variables disappear on build... leading to NullReferenceException error

Hello,
I was trying to build my game today, but my built game is completely broken. The screen is black, the music doesn’t play, nothing happens when I press keys…

After some research I finally found where the problem comes from : it seems as if ALL my variables set from the inspector for ALL of my scripts simply disappear when I build my project ! Nothing works because all of my variables are empty.

  • when I play my game in Unity, everything works fine
  • when I launch my PC built game and I record logs, I can see “NullReferenceException: Object reference not set to an instance of an object” splattered everywhere in the logs…

I’ve never heard of any bug like this… Any idea is welcome !

Update :
Well… I did make a debugging build with breaking points and made some progress :

It looks like my variables are not disappearing, there are just… stuck with old values ? I can recognise values from my previous build (2 month ago). Is it possible for the variables to retain values from previous build even though I changed them in the editor ?

That would explain why the variables I added very recently are empty and cause NullReferenceException → they did not existed in my previous build !


For some more info :

  • I’m using Unity 2020.3, I tried opening and building my project with Unity 2022.3 → same bug

  • I tried Assets > Reimport all → same bug

  • My project is fairly small, I only have one scene

  • I tried creating test variables with different types, but everything comes out as empty variables in my build (booleans, strings, custom class instances, everything !!!)

  • I’m using Github even though I work alone. I saw somewhere that assets references can break if you switch branches. But I reimported everything and it did not solve my bug…

This is very likely the actual issue, not that your “variables disappear”.

See this post: https://discussions.unity.com/t/926887/2

Make a debug build, attach the debugger, set a breakpoint on the line of the very first Nullref in the log and analyze where it’s coming from.

Yes I know that’s the core of the problem.

Didn’t know you can make a debug build and use breaking points ! I’m trying this right away, thank you so much !

Well… I did that debugging with breaking points and made some progress :
It looks like my variables are not disappearing, there are just… stuck with old values ? I can recognise values from my previous build (2 month ago). Is it possible for the variables to retain values from previous build even though I changed them in the editor ?

That would explain why the variables I added very recently are empty and cause NullReferenceException → they did not existed in my previous build !

With Asset Bundles and Addressables it is possible, as updating your asset is its own process separate to your standard build if you don’t have it to configured to run on build. Would you be using either of those?

I don’t use Asset Bundles or Addressables :confused:

Update : I’m just dumb, I forgot I duplicated my scene with a slightly different name. I was just trying to build an old scene lol. Sorry for wasting your time <3