Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Completely Stuck... Problem with Windows Exe Build vs. Editor Behaviour

Discussion in 'Editor & General Support' started by sauerkraut, Jul 18, 2021.

  1. sauerkraut

    sauerkraut

    Joined:
    Feb 21, 2014
    Posts:
    23
    I have a curious problem. I have been working on a 2D game based on, but with heavily modified Corgi Engine for about a year.

    I am getting to the point where I would like to publish this for further community feedback and this requires me to produce a robust Windows Platform build, and I am struggling with differing behaviour between Editor (everything works fine) and Windows build (Has serious issues).

    The game structure is based on three main elements:
    1. A Main Menu Scene which currently allows the user to launch any of the 60+ playable levels
    2. A Common Scene which contains shared Corgi, UI and other elements
    3. 60+ Level Scenes which constitute level specific content, cut scenes, etc.

    The flow is as follows:
    1. Main Menu Scene loads
    2. The Main Menu Scene loads (and is replaced by) the Common Scene
    3. The Common Scene additively loads a single Level Scene, and user plays the level.
    4. User beats the level, and the Common Scene unloads the Level Scene and additively loads a different Level Scene
    5. Rinse, Repeat 3,4 until the user wants to jump forward / backwards in levels and chooses to return to Main Menu Scene. The Common Scene and the currently loaded additively loaded Level Scenes unload and....
    6. Back to item 1

    This flow works perfectly in the Editor and I can perform this cycle endlessly.

    The problem comes in when I build / play the Windows executable.

    Flow works perfectly 1-6 and back to 1, 2. When step 3 happens the second time, the behaviour is completely different. Corgi Engine does not create a player character, and everything at this point goes haywire.

    I have put a lot of debug code through everything, including Corgi Engine and haven't found the root cause yet and it's driving me nuts. There seem to be no new error messages being elicited. The fundamental question I keep having is how can this happen unless Unity is not properly cleaning up between Scene Loads? I have checked all Static classes I have (no luck) and also anything in DontDestroyOnLoad to see if there is any way that stuff could survive Scene unloads / Reloads, but nothing seems to make sense.

    I have wasted many hours on this already. Any thoughts?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    One common source of these sorts of things is unwitting dependence on script execution order.

    Unity does not guarantee order of execution, but often times in the editor it will be rock solid steady in a particular order (say, A, B, C) and then in the build it will be completely different (A,C,B), and fail every time if your code doesn't handle it.

    With additive scene loading it gets even wonkier because scenes do not load until the END of the current frame, introducing more ways for things to go haywire.

    Try this: make changes to your scenes and scripts so that they sometimes do nothing for 1 frame... fix any errors relating to that. One easy way to do this is to have a
    bool ready;
    variable in scripts, then make Start() into a coroutine (simply by declaring it
    IEnumerator Start()
    ) and drop in a few random
    yield return nulls;
    inside of Start.

    Finally at the end of Start() you would set
    ready = true;


    Obviously use the state of the
    ready
    flag to inhibit things like Update() from running.
     
    Last edited: Jul 20, 2021
    Joe-Censored and sauerkraut like this.
  3. sauerkraut

    sauerkraut

    Joined:
    Feb 21, 2014
    Posts:
    23
    Thanks Kurt for your reply.

    In the end it was something very different - Corgi Engine does not seem to like having it's different components split across multiple additively loaded scenes. Even the editor copes with this, once built it caused issues.

    The solution was to reimagine how to structure my scene loading topology. I now have all of the Corgi components in the Common Scene, and the Level Scene now only had has pure content. This required a lot of customisation of the Corgi Engine to conditionally initialise things, which is it not made to do, however it has solved all of my problems above.

    Cheers,
    Stephan.
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Oh very nice: nice tracking to figure that out and glad you were able to rework to make it happy!