Any idea how i can make my timer start after 3 seconds ??
right now i set it in -3 and i cover it with a panel for 3 seconds but i dont like the panel can someone suggest a way to make my timer script start 3 secondes after i start the game?
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Timer : MonoBehaviour
{
public Text timerText;
private float startTime;
public Text hightscore;
private float finaltime;
public GameObject NewHighScore;
void Start()
{
startTime = Time.time;
hightscore.text = PlayerPrefs.GetFloat("HighScore").ToString("f2");
}
void Update()
{
float t = Time.time - (startTime - (-3));
float seconds = (t % 10000);
timerText.text = seconds.ToString("f2");
finaltime = (seconds);
PlayerPrefs.SetFloat("CurrentTime", finaltime);
if (finaltime > PlayerPrefs.GetFloat("HighScore"))
{
PlayerPrefs.SetFloat("HighScore", finaltime);
NewHighScore.SetActive(true);
}
}
}
You can just have a boolean called something like timerEnabled
Then only advance or reduce the timer when that boolean is true.
Also, you don’t have to subtract time from Time.time the way you are doing.
You can instead start with a float variable at 0.0f, and then every frame add to it Time.deltaTime, and it will advance one second per second perfectly. That lets you put the delta time addition inside an if statement easily.
bool timerEnabled;
float MyTimeValue; // this starts at zero. you could give it an initial value in Start()
void Update()
{
if (timerEnabled)
{
// if you want it to count down, just do a -= instead of a += obviously
MyTimeValue += Time.deltaTime;
}
}
And then somewhere else (even in the same function with ANOTHER sub-timer float variable!) you can turn on timerEnabled when three seconds have passed.