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
Update 1: I ctrl+F'd through every single instance of the entire solution with the name of the class. Not ONE instance -- all singleton refs. What is going on.................. Both the dupes have unique instance IDs and even the game object parents have unique instance ids .... yet there's only 1 of each! I even tested for the hierarchy location: both at root, where there's only 1 gameObj with 1 script attached. Ahhhhh! O__o lol
Answer by MrLucid72 · Mar 13, 2017 at 02:23 PM
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.
See attached screenshot
Relevant post @ http://answers.unity3d.com/questions/1325286/async-code-on-scene-change.html#answer-1325401
Hey, just because im curious... where is that "consoleE" window from, that you show in the picture?
Still exists in 2018.2.0 - thanks to your comment I solved my issue with "multiple Awake() call". ;) Edit: In my case I use standard: Scene$$anonymous$$anager.LoadScene(name) because asynchronous loading was redundant. ;)
Answer by ArshakKroyan · Oct 25, 2018 at 07:19 AM
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.
Answer by unity_VvCODKknmBJQGw · Jun 01, 2021 at 04:26 PM
Note that the "wait for 0.1 seconds" is kinda brute... Might wanna use:
await Task.WhenAll(_asyncOperations);
Answer by tomosbach · Aug 02, 2021 at 08:43 AM
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?
Aaaaaah I see this post is actually 3 years old, so I'm guessing there is no resolution on the horizon, eh :'(
Your answer
Follow this Question
Related Questions
Random image appears in the center of the screen while using scrollrect 1 Answer
Big bug in all materials 0 Answers
Play mode enters full screen/ cant exit? 1 Answer
UnityEngine.UI.Image keeps removing itself from a Prefab. 1 Answer
When using a Gameobject variable in a public void in C# it does not show up in button(script) 1 Answer