Search Unity

Noob question about awake and start functions

Discussion in 'Scripting' started by Latchh, Feb 19, 2021.

  1. Latchh

    Latchh

    Joined:
    Aug 10, 2020
    Posts:
    49
    In a built game, are the awake and start functions called during the initial launch of the game, when the scene containing an object they're attached to is launched/loaded, or at some other time?

    Asking because I was initially running everything from one scene, but I want to add a second scene as a title/splash page, but I also need all the awake and start functions in my script to only run when the "game" scene (vs the splash scene) is run.

    Thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    The rule for when stuff runs is entirely based on what is in the scene and enabled. If you have your game stuff in another scene than the main menu, it won't be run until you enter that scene.

    And when you come out and go back in of any scene, of course it all happens again. Changing a scene is like "Okay kids, everybody out of the pool, now, new pool over here!" Unity tears everything down (within some notable exceptions such as DontDestroyOnLoad() ) and puts it all in afresh.

    Separate scenes AWESOME in Unity. Very powerful.

    Also, this is some handy timing diagram information to insert deliciously into your brain:

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
    Latchh likes this.
  3. Latchh

    Latchh

    Joined:
    Aug 10, 2020
    Posts:
    49
    Thanks so much for that reply, very helpful.

    This is irrelevant to me currently but that does raise another question - if I wanted a pause screen does that mean it would be in my best interest for that to be part of my game scene as a UI overlap pop-up on keypress or something, and not constructed as a separate scene?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    I've done both. For yourself, the former is fine, probably the simplest. Just follow any average UI pause tutorial.

    If you're on a big team where everybody works on a chunk of the UI, it might be best to put chunks in different scenes and pull them up when you want.

    You can also unload scenes, or load a scene and then tear chunks of it out to put in another scene or whatever.

    Actually, I wrote many a blurb before about additive scene loading:

    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

    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
     
    Latchh likes this.
  5. Latchh

    Latchh

    Joined:
    Aug 10, 2020
    Posts:
    49
    Stunningly helpful as always Kurt, thanks so much. Already seeing a lot of good potential use. I'll definitely give those links a proper look over the weekend
     
    Kurt-Dekker likes this.
  6. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    For a pause, go with the UI popup. You're pausing the scene in mid-play, so don't load another scene, you'd lose everything unless you take great pains to store the state, then you'd have to re-load it. (With standard scene loading/unloading.) The popup is probably just one element (Canvas with child objects), so a simple enable/disable and you're set.

    I only do additional scenes when there is a solid separation of content, or there are multiple similar levels which can't easily be done procedurally, etc.

    That said, there are lots of ways to do things!
     
  7. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    No, because you could load the pause-scene additively and then your original scene will not unload. Both scenes will be open at the same time.
     
    Kurt-Dekker likes this.