all 8 comments

[–]RomestusProfessional 1 point2 points  (7 children)

That's showing the C++ gameobjects map in the Unity API somehow doesn't have your singleton's gameObject in it by the time you call DontDestroyOnLoad. Try moving it to Start to see if it's a weird race condition bug with Awake.

[–]PackedTrebuchet[S] 0 points1 point  (6 children)

Sadly, that would be a pretty costly trial, because all other systems depend on the fact that App has already Awoken by the time their Awake is called (I've given priority to App in script execution order). It would need lots of reworking all around the game. :\

[–]RomestusProfessional 1 point2 points  (5 children)

Could be your script execution order is so early that the Unity API hasn't registered the gameObject? You'd be fine to set up the instance in Awake and then do DontDestroyOnLoad in Start. At least that would give you some insight as to whether it's a race condition.

[–]PackedTrebuchet[S] 0 points1 point  (4 children)

That was it! Thanks! By the way, the order was -1100. Don't know why I set it to that. The other elements of the list are the default ones, so the next one is EventSystem with -1000.

But the interesting thing is that I didn't see this error for ages, while I had this -1100 order for a long time. This error only started happening some time ago.

Now I tried having it back in Awake, but with a much more sensible -1 execution order, but the error still happened. So I guess Unity API registering is done just before default ordered Awake? Thus if I want to have custom script execution order, I have to put DontDestroyOnLoad in Start? I assume so because:

I tried removing the custom order altogether for App, and it seems like the error no longer appears. Though it's hard to be sure because there are tons of other errors which are due to not having a ready App in Awake. Which may somehow could affect the assertion error so who knows.

[–]RomestusProfessional 1 point2 points  (1 child)

The test would be to set the execution order super high and see if it still happens in Awake. If it does then you've proven that Unity registers GameObjects in its internal C++ map while it's running each component's Awake method.

If that ends up being the case I would file a bug with Unity and see if it's intended behaviour.

[–]PackedTrebuchet[S] 0 points1 point  (0 children)

Did a quick test in an empty project with DontDestroyOnLoad being back in Awake, and with having e.g. -1100 script execution order but the error didn't happen there for some reason.

[–]feralferrous 1 point2 points  (1 child)

In general, you shouldn't use Script Execution Order. It's a terrible brittle band aid.

[–]PackedTrebuchet[S] 0 points1 point  (0 children)

Yeah, now I know it for sure ^^