Null reference on first Instantiate

So here is my problem…

I have a scene with a GameObject which has a script that causes it to instantiate a GameObject that draws parameters from random scriptableobjects every second. The objects destroy themselves shortly after.

Now when I test the scene everything looks perfect. The objects spawn just fine, and I get ZERO errors.

While testing however, if I navigate to a new scene, then BACK to the Original scene with the Spawner, the very first object spawn comes back missing the reference to the scriptableobject. every object after that works perfectly.

But that first object gives me

NullReferenceException: Object reference not set to an instance of an object,

which stacks every frame until the game object destroys itself. The Spawner is on Update()!

Anyone have any ideas what could be causing this to happen?

Without seeing some code and understanding how you have things setup in your scene, it’s difficult to say.

Without code we’re just guessing. So here’s my guess: You are using an Object with Don’tDestroyOnLoad that references an object that does not have that flag set → boom.