Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to wait for Unity to be fully initialised.

Discussion in 'Scripting' started by Paul_H23, Sep 10, 2020.

  1. Paul_H23

    Paul_H23

    Joined:
    Jun 19, 2019
    Posts:
    45
    Hitting a weird problem, wonder if anyone here has any insights. The game I have is heavily server oriented, it has to connect to a Firebase server and download various data from the realtime database, as well as downloading AddressableAsset bundles from Firebase Storage at first startup. The config is simple, I have a boot scene, that does little more than show the game logo, and initiate the server setup, and then, when enough of it is completed, load the main scene.

    The problem I'm having is, if I initiate the network stuff in Start() on a GameObject in the boot scene, it often stalls during startup, either not showing the Unity logo, or flashing the Unity logo constantly, but never seeing the game logo which is in the boot scene. I can see all the network stuff happening in the logs through XCode, but the game never shows because it's always showing the black loading screen.

    However, If I put a 10s delay at the start of that method, it seems to work. So I presume there is some setup that needs to happen at start, that network activity is conflicting with. Adding a 10s delay is a) very arbitrary, and b) means that even if there is no downloading to be done, i.e. subsequent loads, they player has to wait for nothing.

    Anyone got any idea how I can wait more reliably for Unity to be fully initialised and ready to go before I kick off the network connection stuff?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    My approach is that 100% of my games have a first scene called
    zeroscene
    and all it does is have a
    zeroscene.cs
    script with a Start() method in it that changes to the next scene, usually the
    initialize
    scene or the
    mainmenu
    scene. I usually change the camera in
    zeroscene
    to just clear to
    Color.black
    , and there's nothing in the scene to see anyway.

    Once the game is started, I never go back to the
    zeroscene
    .

    What that does is give the entire Unity engine main loop a chance to run before you try doing anything more. This has gotten me out of all my woes as far as startup timing between me and the engine components.
     
    Joe-Censored and Paul_H23 like this.
  3. Paul_H23

    Paul_H23

    Joined:
    Jun 19, 2019
    Posts:
    45
    Thanks, that seems to be working. I'm not sure it's a "great" solution, in that surely Unity should have a way to do this properly, isn't it reasonable to expect to have some sort of "Ok, I'm (Unity) am all done setting up, go do your stuff now" event?

    However, in the interim, until someone tells me there is a better way, this seems to solve my immediate problem (fingers crossed), thanks.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    I don't think Unity should do ANYTHING with your code until it is 100% setup for business, but in your specific bug, it might be related to that firebase library not yet getting its first "Start()" or "Update()" or something, so in that case it's out of Unity's hands.

    I started doing the
    zeroscene
    trick back in 2013 when I found that on Android targets, Unity misreported the screen resolution as 640x480 (via Screen.width and Screen.height) in the Awake() method on the first frame of the game. I filed a bug and it got fixed pretty quickly.

    Once I started doing the
    zeroscene
    trick (and I always use
    Start
    in there, not
    Awake
    !), it just seemed like a good "wart" to put on everything, just to save time wasted on silly engine and initialization bugs.
     
    Paul_H23 and Joe-Censored like this.
  5. Paul_H23

    Paul_H23

    Joined:
    Jun 19, 2019
    Posts:
    45
    Nope, a week later, and it's all gone pear shaped again. It seems that anything on iOS that doesn't involve loading the simplest of scenes only, no additional scene loading, no network activity, no animations via the Animator or code, anything, is just too much for Unity on iOS. It just doesn't work reliably, sometimes it'll work, sometimes it just sits there flashing the logo at me like some demented raver, laughing at me.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    When it fails, what do you see in the console log from the device?

    This assertkion would be counter to my personal experience as well as counter to a lot of fortune 500 companies making a lot of money off of Unity-authored games on iOS.

    This to me sounds like you inadvertently made logic dependent on run order between different scripts, such as reaching out in an Awake() method to manipulate anything NOT on the current GameObject, which is definitely a great source of unreliability, or else perhaps using any of the System.IO.File calls, which are of course completely off-limits for reading assets out of Unity on device.
     
  7. Paul_H23

    Paul_H23

    Joined:
    Jun 19, 2019
    Posts:
    45
    I've had to just simplify to the extreme, doing away with any good architectural practices I had, such as separation of concerns. Now, everything is bundled into one huge scene, and the order of initialisation is hard controlled via execution order settings and various WaitForEndOfFrame's thrown in for good measure. It seems that, for some reason, anything involving Firebase and multiple scenes is a no-no.

    By far the most frustrating part is that the behaviour in editor is completely different to on-device, it works absolutely fine 100% of the time in editor, but fails in unpredictable ways on device, where it's 1000% more difficult to debug.