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

question about loadscene and awake/start functions

Discussion in 'Scripting' started by Latchh, Mar 25, 2021.

  1. Latchh

    Latchh

    Joined:
    Aug 10, 2020
    Posts:
    49
    I have a question about loadscene that I couldn't find the answer to in the unity documentation.

    If you are using loadscene(), what is actually going on? I am guessing that all the objects in the scene are loaded, but do any of the functions in their attached scripts such as awake or start run as well?

    I am asking because I need to be able to have some scripts affect objects in a scene before the first frame of the scene is run. I have tried just using Awake() and Start() for this but for some reason, certain things I do in awake/start sometimes occur after I call a function in update using an if statement with a bool to ensure it only occurs once (i.e. on the first update frame).

    thanks
     
  2. Bosozuki

    Bosozuki

    Joined:
    Dec 19, 2013
    Posts:
    63
    Start here on the unity manual Order of execution Middle of the page below the flow chart will answer some of your question.

    So.. The real question that will come up will be what happens when you use multi scenes loaded together, async loading and unloading, don't destroy on load etc.

    Every game is different but our approach has been to be very careful with Awake/ and start.

    For our game all instantiated objects and dynamic scene objects(that require access to other scene objects and instantiated objects) are set to inactive in their respective awake calls. We then have another manager type class that sets data and then calls and initialize function in a set order and then everything is set to active. This way we can guarantee order of execution and not have an object access something that is not ready in the scene yet.

    There are ramifications to the above so you really have to pay attention to the data flow of your game.
     
  3. Latchh

    Latchh

    Joined:
    Aug 10, 2020
    Posts:
    49
    I'm still quite new to unity so maybe I didn't understand something, but I'm still uncertain about my original question.

    I've seen the order of execution chart before and I understand in theory when things are meant to run (even if in my own experience I've noticed things happening in update before something has finished in awake), however what I don't understand is when awake and start get called when using loadscene() - if at all.

    I probably should have provided a bit of additional context - I want to be able to load the objects in my next scene ahead of time, and have whatever methods I would be running in awake/start to occur when those objects are loaded in (or after looking at order of execution again, awake and onenable seems more appropriate). So given what you said, I guess my question becomes does Unity automatically run awake/onenable/start functions when an object is loaded with loadscene, even if the scene technically hasn't started yet, or do I need to manually prepare whatever I wanted to do in awake/start with something like a manager class.

    Also, the whole reason I'm splitting things between awake/onenable/start is because I am trying to ensure a specific order of execution, however the way you described sounds a lot better. Do you have any links to documents or example code I could look at to better understand how to implement that?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    Scenes don't load until the current end of frame, so everything gets called in the NEXT frame after this one.

    Once you master the timing, often by making your scripts tolerant of objects not being ready for one cycle, additive loaded partial scenes is amazingly powerful.

    I have blathered about this before... perhaps some of it might be of insight to you?

    Additive scenes are one possible solution:

    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6630961
    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6754330

    https://forum.unity.com/threads/problem-with-canvas-ui-prefabs.1039075/#post-6726169

    A multi-scene loader thingy:

    https://pastebin.com/Vecczt5Q

    My typical Scene Loader:

    https://gist.github.com/kurtdekker/862da3bc22ee13aff61a7606ece6fdd3

    Other notes on additive scene loading:

    https://forum.unity.com/threads/removing-duplicates-on-load-scene.956568/#post-6233406

    Timing of scene loading:

    https://forum.unity.com/threads/fun...ject-in-the-second-scene.993141/#post-6449718
     
  5. Latchh

    Latchh

    Joined:
    Aug 10, 2020
    Posts:
    49
    Ok so just to confirm, when I call loadscene(), the next scene will (begin to? finish?) load in the following frame, and during that load the awake and start functions I have in any script attached to an object in that scene will also get called?


    Amazing, I'll have a read through those, I'm sure I'll learn something for the future if not a better way to achieve what I'm trying to do at the moment. Although currently because I am working by myself, I only need to have a title screen scene then a main scene, and I only need to load everything once then never load anything again per session (and I don't care how long it takes to load the main scene), I am not sure if additive scenes are necessary for me.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    Human-readable language is just wiggly-wobbly enough that you should just make a script with Awake() and Start() methods in it, put it into two scenes, and another script to load from one scene to the second. Within 2-3 minutes of typing you will have an actual irrefutable reference to how the timing works. Throwing up super-quick tests in Unity is really the best way to make sure you understand something as subtle as this kind of timing.

    But still: make your scripts tolerant of divergence in this timing, at least to a reasonable extent!!
     
    Latchh and Bosozuki like this.
  7. Latchh

    Latchh

    Joined:
    Aug 10, 2020
    Posts:
    49
    So this was great advice, because it turned out things didn't work quite the way I expected them to and now I have a new question (I realized I've already been using loadscene elsewhere so I really should have expected it to behave as it does, but oh well still learning).

    After reading through more Unity docs and your links, and doing some testing, I've started to get my head around single and additive scene loading, what you can do with async loading, etc. However I am wondering if there is a way to load a scene, but require a player input to initiate the first Update()? I want to essentially get stuck in the loading scene after the load is completed until the player presses a button.

    Alternatively it seems like my best option would be to do everything that I'd do in Awake/Start() of my main scene in a menu scene instead, and pass everything to the content scene with a static class script but my concern is that this would still lag my first frame (or few frames) of the main scene which is exactly what I am trying to avoid.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    Sure, buncha ways: implement a pause feature, start the game paused

    OR... just load everything, have everything come up disabled, wait for the press, then enable.

    Or... any number of any other ways.
     
    Latchh likes this.