Timer starts from wrong value on void Start or Awake?
So I have a strange question. I am absolute beginner and I may be misunderstandign things but this is timer code I use:
public class BasicTimer : MonoBehaviour
{
public float timeRemaining = 10;
public bool timerIsRunning = false;
void Start()
{
// Starts the timer automatically
timerIsRunning = true;
}
void Update()
{
if (timerIsRunning)
{
if (timeRemaining > 0)
{
timeRemaining -= Time.unscaledDeltaTime;
Debug.Log(timeRemaining);
}
else
{
Debug.Log("Time has run out!");
timeRemaining = 0;
timerIsRunning = false;
}
}
}
}
All seems fine and dandy... however when I click play in editor, the value doesn't start at 10 but 4 less (yes, I have checked inspector). If I however make it so player has to click left mouse button in the update, it starts at correct value.
The reason I use "unscaledDeltaTime" is because my game is sped up slightly in project settings to 1.3 Time Scale (it is required).
Any reason why timer is off by 4 seconds on Start() and Awake()?
Answer by Monsoonexe · Feb 21 at 12:51 AM
Is it possible that the time gap is due to the time it takes to start Unity/enter PlayMode? The first frame might be taking extra long due to loading time.
That may be it indeed, however I am really unsure how to test that without building the game, but I suppose it should be fine. Thanks!