Hey guys, I have a timer in my game which should count the time till the end of the game.
I can’t figure out how to make it so it will only reset back to 0 when menu scene is active. ( build index 0)
The second problem is that I don’t want it to go back to 0 after every death(scene reload).
Have you got any sugestions?
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Timer : MonoBehaviour
{
public Text TimerText;
public bool shouldCountTime;
public float t;
private void Start()
{
if (SceneManager.GetActiveScene().buildIndex != 0)
{
shouldCountTime = true;
}
else
{
shouldCountTime = false;
t = 0;
}
t = Time.deltaTime;
}
void Update()
{
if (shouldCountTime) {
t += Time.deltaTime;
}
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
TimerText.text = minutes + ":" + seconds;
}
}