Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Moving a Time/Timer Across Scenes

Discussion in 'Scripting' started by Soraya_C, Sep 3, 2020.

  1. Soraya_C

    Soraya_C

    Joined:
    Feb 12, 2020
    Posts:
    2
    Hey everyone! I'm having trouble with how to script a time created in the first scene to appear in the second scene. I can switch scenes just fine, and the timer is working in the first scene, but I can't seem to find a tutorial that helps with my problem. If anyone has any idea how to do this, please let me know! :)
     
  2. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    692
    A few ways, have it attached to an object with DontDestroyOnLoad, or make the timer function static, so it's not bound to an object and will persist across scenes. This is also a way to manage variables like "score" which need to persist.
     
  3. Crossbreed

    Crossbreed

    Joined:
    Jul 5, 2016
    Posts:
    10
    I assume this is a timer that is displayed for the user? Or is it something to tell if like the user beat the game in X time kinda thing?

    DontDestroyOnLoad might work either way. Static timer could also work. You could also just record the time to data that is updated on save or on scene exit.
     
  4. Soraya_C

    Soraya_C

    Joined:
    Feb 12, 2020
    Posts:
    2
    Thanks so much both of you! I used DontDestroyOnLoad and it ended up working!
     
    Cyber-Dog likes this.