I want to know what would be the best solution to save times on my timer here.
I used this tutorial as a basis for my timer:
Timer code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
//using TMPro;
public class Timerscript : MonoBehaviour
{
public GameObject Horns;
public float timeInMinutes;
public float timeInSeconds;
private float timerduration;
public GameObject GameOverScreen;
public Transform trackedObject;
[SerializeField]
private bool countDown = true;
public float timer;
[SerializeField]
private Text firstMinute;
[SerializeField]
private Text secondMinute;
[SerializeField]
private Text seperator;
[SerializeField]
private Text firstSecond;
[SerializeField]
private Text secondSecond;
private float flashTimer;
private float flashduration = 1f;
void Start()
{
/* The plus one increment is due to the timer always incrementing by one second when the game first starts*/
timerduration = timeInMinutes * 60f + timeInSeconds + 1f;
ResetTimer();
}
// Update is called once per frame
void Update()
{
if (trackedObject != null)
{
if (countDown && timer > 0)
{
timer -= Time.deltaTime;
updateTimerDisplay(timer);
}
else if (!countDown && timer < timerduration)
{
timer += Time.deltaTime;
updateTimerDisplay(timer);
}
else
{
flash();
Object.Destroy(Horns.gameObject);
Cursor.lockState = CursorLockMode.None;
GameOverScreen.SetActive(true);
}
}
else { /*Debug.Log("Horns is Dead"); return;*/ }
}
private void ResetTimer()
{
if (countDown)
{
timer = timerduration;
}
else
{
timer = 0;
}
setTimerDisplay(true);
}
private void updateTimerDisplay(float time)
{
if (time < 0)
{
time = 0;
}
float minutes = Mathf.FloorToInt(time / 60);
float seconds = Mathf.FloorToInt(time % 60);
string currentTime = string.Format("{00:00}{1:00}", minutes, seconds);
firstMinute.text = currentTime[0].ToString();
secondMinute.text = currentTime[1].ToString();
firstSecond.text = currentTime[2].ToString();
secondSecond.text = currentTime[3].ToString();
}
private void flash()
{
if (countDown && timer !=0)
{
timer = 0;
updateTimerDisplay(timer);
}
if (!countDown && timer != timerduration)
{
timer = timerduration;
updateTimerDisplay(timer);
}
if (flashTimer <=0)
{
flashTimer = flashduration;
}
else if (flashTimer >= flashduration / 2)
{
flashTimer -= Time.deltaTime;
setTimerDisplay(false);
}
else
{
flashTimer -= Time.deltaTime;
setTimerDisplay(true);
}
}
//public string GetTimeString()
//{
//return TimeFormatter.Format(currentTime);
//}
private void setTimerDisplay (bool enabled)
{
firstMinute.enabled = enabled;
secondMinute.enabled = enabled;
seperator.enabled = enabled;
firstSecond.enabled = enabled;
secondSecond.enabled = enabled;
}
}