Time.time stops working after reloading a level

Hi everybody, I’ve been having problems with this for quite some time, and I can’t get my head around it. The basic idea is quite simple: have a countdown timer at the beignning of each level. It works quite well, and even allows you to pause the game during the countdown and it will respect that. The problem I’m having is after reloading the scene. The first time it runs, it works perfectly, however the second time, Time.time stops counting. I’ve printed the timer on console and what it shows me is that Time.time simply stops after laoding the scene. I’ve tried using fixedTime, timeSinceLevelLoad and the regular time.

Interestingly, it starts working when I use realTimeSinceStartUp. However, this causes other isues (such as beign unable to pause the game on countdown).

Does anyone know why time.time stops counting after reloading the scene?

Here is my code:

  using UnityEngine;
    using System.Collections;
    
    public class GamePrep : MonoBehaviour {
    	
    	public bool useTimer;
    	public int countdownTime;
    	
    	//the time when the script is called
    	private float startingTime;
    	
    	//a buffer variable for time operations
    	private float guiTime;
    		
    	//the number you will display
    	private int displaySeconds;

    	
    	// Use this for initialization
    	void Start () {
    		
    		
    		#region Timer initialization
    		//if we are in prep mode and are going to use the countdown time
    		if(useTimer){
    			
    			//initialize seconds
    			displaySeconds = countdownTime;
    			
    			//save the time
    			startingTime = Time.time;	
    
    		}//end if
    		#endregion		 
    		
    	}//end start
    	
    	void OnGUI(){
    		
    		guiTime = Time.time - startingTime;
    						 
    		print(Time.time + " : "+ startingTime);
    		
    			#region Timer	
    			//start the timer
    			if(useTimer )
    			{
    			
    				displaySeconds = Mathf.CeilToInt( countdownTime - guiTime);
    
    			}//end if
    			#endregion	

    			//if the timer has reached 0
    			if(displaySeconds <= 0){
    				
    				//when the countdown stops; change to play mode
    				GameMode.currentMode = GameMode.gameMode.play1;
    			
    			}

    	}//end mehtod
    	
    }

I moved the time keeping to an update method, but to no avail. I was thinking it might have something to do with the timeScale. See, restarting the level comes from the pause menu, which sets the timeScale to 0. Is it possible that after loading the scene, the timeScale is not reset to 1?,Thanks for the answer. I moved everything to the update, however it still behaves the same way. A thought occured to me, but I haven’t been able to test it. In order to reload the level, I first have to pause the game, which sets the timeScale to 0. Can it be possible that the timeScale is not resetting to 1 after reloading the scene?

I’m not sure about using Time in OnGUI, and putting print in there, oooh. That gets called several times per frame you know. You can move the Time keeping to Update and use just those variables you set in OnGUI, and use a GUI.Label to print Time.time if you need to.
That said, I don’t know why it would behave that way, and differently on different runs.

I managed to make some tests on that last idea and its now definitly working. Thank you for your attention.

I think you should reset the time in the beginning of each level, and call the function wherever necessary.