Search Unity

On Application Start function?

Discussion in 'Scripting' started by YevgenyBlinov, Dec 8, 2018.

  1. YevgenyBlinov

    YevgenyBlinov

    Joined:
    May 27, 2018
    Posts:
    6
    I need something to happen not on Start/Awake, and not when a scene is loaded, but only once the application is launched. It means that if I reload the scene, the function WILL NOT be called. PlayerPrefs doesn't work here too, because I DO want the function to be called again the next time the player launches the app. Is this possible?
     
    MaxLohMusic and umarhyatt like this.
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    What I do is have a boot scene, which has what I want to stay persistent, and use DontDestroyOnLoad on the objects in the scene in the awake function, then I load the next scene which might be the menu or whatever. Don't know if that's what you mean or not. There is a video on persistence in the learn tutorials.
     
    Bunny83, Slashik, Ryiah and 1 other person like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Yeah, this is definitely the winner way on Unity. Every one of my games has a "zeroscene" and it handles all the "I only want this to happen once" stuff.

    Alternately if you don't want to be reliant on going to a zeroscene every time you press play, you can implement one of the various ways of making Unity Singletons. Here's some more info on those:

    http://wiki.unity3d.com/index.php/Singleton
     
  4. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    MY starter scene is called "Init". I think a lot of developers use those :)

    It loads manager stuff, forwards to a menu scene or primary scene, and is never seen again until the app is launched again.
     
  5. YevgenyBlinov

    YevgenyBlinov

    Joined:
    May 27, 2018
    Posts:
    6
    Doing an init/boot/zero scene that won't be seen again, sounds like a great idea. :)
    This is exactly what I need.
    Thank you guys!
     
    Kurt-Dekker and Lurking-Ninja like this.
  6. Also if you want quick boot up, you can create a small temporary script, where you show a selector to select a level and upon start after the boot you can load that scene directly. So you keep the good things from both worlds, boot scene and "direct" play in a specific scene.
     
    Kurt-Dekker likes this.
  7. leo-barreto

    leo-barreto

    Unity Technologies

    Joined:
    Jun 3, 2021
    Posts:
    1
    Bunny83, KeaneC and Combat-Wombat like this.
  8. KeaneC

    KeaneC

    Joined:
    Sep 15, 2018
    Posts:
    1
  9. unity_CF14CD16A64ADE413915

    unity_CF14CD16A64ADE413915

    Joined:
    Sep 11, 2022
    Posts:
    4
    you could just say in void Update
    if(Time.time == 0f)
    Debug.Log("!");

    will be called once the game has started even if you are making the class [ExecuteAlways]

    because Time.time will not be zero in edit mode, but will be 0 on game start
     
  10. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,157
    That's a fantastic way to introduce a nearly untraceable bug. If you must rely on an approach like that you should use a variable that you have full control of and not one that will be modified outside of the script that depends on an arbitrary value. Time.time is not guaranteed to be 0 in Update() on the first frame.

    Code (csharp):
    1. bool isGameStarted = false;
    2.  
    3. void Update()
    4. {
    5.     if (!isGameStarted)
    6.     {
    7.         isGameStarted = true;
    8.  
    9.         // do stuff
    10.     }
    11. }
     
    Last edited: May 19, 2023
    AlexTuduran, Kurt-Dekker and Bunny83 like this.