I have a stop watch, it’s a pretty simple code, here’s the script,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class StopWatch : MonoBehaviour
{
private float timer;
public float defaultTime = 20;
bool countingDown;
public TextMeshProUGUI timeText,startStopText;
public AudioSource click, timerCountingSnd, timerEndSnd;
void Start()
{
timer = defaultTime;
}
void FixedUpdate()
{
if (countingDown && timer > 0)
{
timer -= Time.deltaTime;
}
if(timer < 0)
{
timer = 0;
if (countingDown)
{
timerEndSnd.Play();
StartTimer();
}
}
timeText.text = Mathf.CeilToInt(timer).ToString();
}
public void StartTimer()
{
if (countingDown)
{
StopTimer();
}
else if (timer > 0)
{
timerCountingSnd.Play();
countingDown = true;
startStopText.text = "Stop";
}
}
public void StopTimer()
{
timerCountingSnd.Stop();
countingDown = false;
startStopText.text = "Start";
}
public void IncreaseTime()
{
click.Play();
if (timer > 990)
{
timer = 990;
}
timer += 10;
timeText.text = Mathf.CeilToInt(timer).ToString();
}
public void DecreaseTime()
{
click.Play();
timer -= 10;
timeText.text = Mathf.CeilToInt(timer).ToString();
}
public void RestartTimer()
{
if (!countingDown)
{
click.Play();
timer = defaultTime;
}
}
}
The 4 methods on the bottom are used in buttons. I have a strange issue though, I have never encountered it personally in my own testing, but when I send it to my friend, he said the timer occasionally stops randomly. He set it for 3 minutes and it stopped at 46, the buttons text still said stop so logically the timer should’ve been running, but it wasn’t.
I can’t for the life of me figure it out, I’ve tested it a million times, the timer never stops at anything either than 0. In both cases it’s a Windows 10, so it shouldn’t be a platform issue or anything.
Does anyone have any idea what’s going on here?
Thanks