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

How to restart countdown when new scene loads

Discussion in 'Scripting' started by AlaMo12, Jun 6, 2019.

  1. AlaMo12

    AlaMo12

    Joined:
    Jun 6, 2019
    Posts:
    2
    Hi everyone,

    I am new to coding and Unity. I have been making an incremental game and wanted the scene to change when the score goes from 10 to 0 via an automatic countdown. I have been able to make the scene change once it reaches 0. I have used the DontDestroyOnLoad script to ensure the automatic countdown is maintained from one scene to the next. However this causes the score to go negative as the score needed to change the scene does not reset back to 10.

    I was wondering how to make the score needed to reset back to 10? Maybe by destroying the script after each scene so it can restart the script on the next scene?

    Thanks
    A
     
  2. BTStone

    BTStone

    Joined:
    Mar 10, 2012
    Posts:
    1,422
    Well, after you load the scene just get again access to the counter-script and reset the counter to 10?
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Add a method to your script to reset the score. In all your scenes have a script which finds your score object and calls that method. Or use SceneManager.sceneLoaded to call this method automagically.
     
  4. AlaMo12

    AlaMo12

    Joined:
    Jun 6, 2019
    Posts:
    2
    Thanks for both your replies, as a coding noob I was unsure what a counterscript was.

    I have managed to fix it by placing the code under void awake similar to the dontdestroyonload function.
    It seems to be loading fine at the moment. Do you think any issues will arise.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    BTStone was just referring to whatever script contains this counter you are using to count down. A counter-script isn't some special dev lingo.

    Depends. Hard to say without seeing the code. If it works, you're probably ok though.

    Awake is generally for any initialization or setup for the script containing the Awake method, and Start (which is similar but called after all the Awake methods) is intended more for interacting with other scripts. So if you're interacting with other scripts in Awake it could potentially be a problem since you don't know if the other script's own Awake has been called or not yet.
     
    BTStone likes this.