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

Keeping a countdown active while switching to a different scene

Discussion in 'Getting Started' started by FlowerBoii, Jan 23, 2019.

  1. FlowerBoii

    FlowerBoii

    Joined:
    Jan 14, 2019
    Posts:
    2
    Hello, I am new to unity but trying to create my first "game", mostly for self-education purposes, with a hope that one day I'll be able to at least entertain myself :D

    So the problem I've run into I can only describe as:
    In my second scene, I have "quests" for the player, which once clicked begin a countdown for a specific time, afterwards giving the player rewards such as exp and gold.
    However I have realised that if at any point during the countdown I switch back to my first scene it gets interrupted, and when I turn back to scene2 it's like it was never initiated in the first place.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You need to keep your countdown timer alive across the scene changes. Call DontDestroyOnLoad on it (and note that this requires that object to be at the root level of the scene hierarchy, i.e., its transform.parent must be null).

    You might also want to look at other modes of loading a scene. The Scene Manager can load a scene asynchronously (in the background), and even add it to the current scene rather than replacing everything. Not sure what's best in your situation, but read up on it and see what works for you.
     
    Ryiah likes this.
  3. FlowerBoii

    FlowerBoii

    Joined:
    Jan 14, 2019
    Posts:
    2
    Code (CSharp):
    1. public class SceneSwitchingButtons : MonoBehaviour
    2. {
    3.     public Animator animator;
    4.     public void Button1Click()
    5.     {
    6.         SceneSwitcher.levelToLoad = "Scene1";
    7.         animator.SetTrigger("FadeOutTrigger");
    8.     }
    9.  
    10.     public void Button2Click()
    11.     {
    12.         SceneSwitcher.levelToLoad = "Scene2";
    13.         animator.SetTrigger("FadeOutTrigger");
    14.     }
    15. }
    16.  
    So where in this code would I insert the DontDestroyOnLoad and how does it work exactly? I've never heard about such a command
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    So I must ask, where have you looked to learn more about DontDestroyOnLoad?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Honestly I would go with additive scene loading these days. It offers finer control of when something is loaded and unloaded. DontDestroyOnLoad takes a bit of a sledgehammer approach.

    Well the scripting reference has some terrible examples for beginners. https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

    The learn section is slightly better if you know exactly which key words to search for. https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data
     
    Ryiah and Schneider21 like this.
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Not my point. I'm trying to encourage people to look for themselves, and not just throw up their hands and ask before looking. Google is your friend!
     
  7. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    306
    if you go with the "dontdestroyonload" approach make sure the scene with the script doesn't load twice, maybe just add a empty scene that loads only once in the application start which contain all the "dontdestroyonload" scripts...
     
    Schneider21 likes this.
  8. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    It's something of a dirty word for purists, but you can also look into implementing the Singleton pattern to ensure you always have one and only one instance of an object.

    I've tried to move toward the more "correct" approaches, but nothing beats the ease and simplicity of a Singleton for managing certain things.