- Home /
timer that continues through all scences
So we have a 2D side scroller game using c#. basically your score is how fast you can complete the game. We have a timer script that displays the time but it restarts when it changes to the next level. Also at the end of the game we want the win screen to be able to display your final time! Any help/clues as to how when can achieve this would be great! Thanks!
Answer by fafase · Nov 10, 2014 at 11:06 AM
Call on the DateTime class to see what is the time and record it in a static variable. Then check what is the new time and compare and store:
private static DateTime ? previous = null;
void Start()
{
if(previous != null)
{
DateTime now = DateTime.Now;
// Compare now with previous using milliseconds or second or minute
previous = now;
}
}
Answer by _Adriaan · Nov 10, 2014 at 11:01 AM
Add DontDestroyOnLoad(this) to the Start() or Awake() function of the script that tracks the score. DontDestroyOnLoad(object) will make sure that the gameobject doesn't get destroyed when you load another scene. Just remember that all other variables on the script, and all other components on the gameobject that has your script, also don't get destroyed.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Making a Timer Out out of 3D Text using C#. 1 Answer
How to realize accurate time counter (millisecond precision) 3 Answers
Start timer on mouse0 click 1 Answer