Start()/Awake() being called twice, after already checking for dupes!

See the image below – Pretty much, I have LobbyUI and LobbyMgr gameObjects at top level of the hierarchy.

In each resides lobbyUI.cs and LobbyPhoton.cs, respectively.

When I start the game, TWO awake() and TWO start()'s happen.

I tried to find the path on awake in the hierarchy – well, it’s at /LobbyUI and /LobbyMgr at the top! That’s normal. Check for dupes? Nothing there… nothing unusual…

Well, what about on awake, if the singleton is null, assign - else destroy. IT’S CLEARLY NOT NULL, YET SKIPS THE IF (S == NULL) LOGIC!! …What…?!?

I’m so confused lol halppp

Found the answer – TL;DR caused by async tasks not finishing up before loading (it sounds unlikely, but I swear it and reproduced/fixed it) - if I wait for them to finish (or the ghetto version of waiting 0.1sec before changing scenes), EVERYTHING is resolved.

Unity needs better garbage collection for unfinished async tasks on change scene.


(I reported the bug after being able to reproduce it every time)

What happened
If you change scenes while async tasks are still going, it will bug the hell out of the next scene. I had 2 completely irrelevant scripts that would awake() twice, sometimes even times.

They were confirmed multiple instances of these 2 scripts (lobbyUI and lobbyPhoton), including the parent gameObj’s (LobbyUI and LobbyMgr). I would even trace where they are in the hierarchy and it’d always say top level at /LobbyMgr and /LobbyUI, where I would always see only ONE.

Not only that, ALL USES were singletons so the situation is impossible unles I dropped it in 2 gameObj’s by accident, but I’d “find all references within scene” and nope – just the 1’s.

On Awake() if I tested for null for the singleton, it would SKIP the logic (it was clearly NOT null) and make a singleton of itself, anyway.

This is a HORRIBLE HORRIBLE bug that took me days to debug.

RESOLVED by waiting for 0.1 second before swapping scenes. Literally this was the solution. I found out there were some unfinished async tasks. Surely the non-lazy resolution would be to check all of the callbacks to ensure they are all done.

Either way, Unity needs better garbage collection when changing scenes for async tasks that are incomplete.

alt text

Hi.
Recently I also had such an issue.
Awake() was called twice when I exited from play mode.
After some research through SmartGit change-logs found out that the error was after I had added
[ExecuteInEditMode] for the Script.

I hope this will help others also.

Note that the “wait for 0.1 seconds” is kinda brute… Might wanna use:

await Task.WhenAll(_asyncOperations);

I. Feel. Your. Pain.

I spent howwwwwwwuuuuurrrrrsssssss trying to resolve this problem, before realising that Unity was calling up the scene twice because of Async, despite me having copied the API example word for word. This meant that all of my events kept breaking because they were trying to call up destroyed objects from the first instance of the scene.

Thank you so much though, your post probably saved me from spending the rest of my life in a mental instituion.

@Unity. WhatsUpWithThat?