heyJordanParker/yaSingleton

NullReferenceException on first Play

Closed this issue · 2 comments

As discussed on Twitter I'm writing you my problem here and I made a repro project here for you as well to check out to see what's going on. Perhaps it is a user error, I do not know.

Link to repro: https://github.com/Spreadcamp/yaSingleton-Repro
Hope I did it right, it's my first repository here.

Close the Editor after first open and reopen, then go into Playmode and you should see a NullReferenceException in the Log. This is on 2020.1.6f with HDRP, I havn't tried other versions other than 2020.1 or other render pipelines. Then when stopping Playmode and going into it again, the NullReferenceException disappears and everything just works.

This is of course a quickly put together example, in my real project I try to get references to Button Prompt Sprites through another Scriptable Object, Video Settings etc and it happens all the time preventing the gameplay code to execute until I run Playmode a second time.

Looks okay, I'll boot it up and see what the issue is right away!

Okay, here's what's happening:
Unity has this feature called Preloaded Assets - it basically ensures that some assets (e.g. your singletons) will be loaded when you publish your game. It seems in the most recent versions of Unity - they do not get preloaded in the Editor.

I just deployed a fix for that so if you update your codebase to the most recent yaSingleton - you will not have any issues. The updated version basically fetches all preloaded assets which in turn initializes them. Now, they behave in the same way in the editor as they would in a published game.

You can also just manually replace the contents of your Assets/Scripts/yaSingleton/Utility/PreloadedScriptableObject.cs file with the ones you will find here: Updated Version.

Thanks for reporting that bug and feel free to post other ones if you have issues!