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
}
Thanks set my Time.timeScale = 1 in Start() and now everything works :)
– Combinacijusx