I’m using unity UI to make an android app, a very simple one. You can choose how many minutes and hours you want, and it turns them into a seconds-countdown.
It works pretty well, however when the sum of hours and minutes (the seconds) is too large, the countdown is a lot faster than it should be.
using UnityEngine;
using UnityEngine.UI;
public class MainCode : MonoBehaviour
{
private float seconds;
private float textSeconds;
private float minz;
private float hourz;
public Text timeText;
public Text minutesText;
public Text hoursText;
private bool countDown = false;
private bool countUp = false;
private bool canSlider = false;
public Slider slider;
public GameObject pauseSign;
private bool pauseSigner = false;
void Update()
{
timeText.text = textSeconds.ToString();
minutesText.text = minz.ToString();
hoursText.text = hourz.ToString();
textSeconds = Mathf.RoundToInt(seconds);
if (canSlider) { slider.value = seconds; }
if (seconds <= 0f) { seconds = 0f; }
if (minz == 0) { minutesText.text = " Minutes . . ."; minutesText.alignment = TextAnchor.MiddleCenter; }
if (hourz == 0) { hoursText.text = " Hours . . ."; hoursText.alignment = TextAnchor.MiddleCenter; }
if (countDown) { seconds -= Time.deltaTime; }
if (countUp) { seconds += Time.deltaTime; }
if (pauseSigner)
{
pauseSign.SetActive(true);
} else { pauseSign.SetActive(false); }
}
void Awake()
{
Application.runInBackground = true;
}
public void Plus60()
{
if (minz <= 999f)
{
seconds += 60f;
minz += 1f;
}
}
public void Minus60()
{
if (minz > 0)
{
seconds -= 60f;
minz -= 1f;
}
}
public void Plus3600()
{
if (hourz <= 999f)
{
seconds += 3600f;
hourz += 1f;
}
}
public void Minus3600()
{
if (hourz > 0)
{
seconds -= 3600f;
hourz -= 1f;
}
}
public void Startz()
{
if (seconds > 0f)
{
GameObject.Find("Panel").GetComponent<Animator>().Play("StartClick");
countDown = true;
canSlider = true;
slider.maxValue = seconds;
}
if (seconds <= 0f)
{
GameObject.Find("Panel").GetComponent<Animator>().Play("StartClick");
countUp = true;
}
}
public void Pausez()
{
pauseSigner = !pauseSigner;
countDown = !countDown;
}
public void Restartz()
{
GameObject.Find("Panel").GetComponent<Animator>().Play("RestartClick");
countDown = false;
countUp = false;
canSlider = false;
pauseSigner = false;
slider.value = 0f;
slider.maxValue = 0f;
seconds = 0f;
minz = 0f;
hourz = 0f;
}
}
I’ve gone over it a few times, and I couldn’t find any errors. (It’s very simple and not so well done, I’m kinda new to coding).