I created a reverse timer for the system of lives. i.e., life should be added after 00:00 if the field is empty. I need the timer to work before all the hearts are full, i.e. when one heart is full, the timer turns on again, if all the hearts are in place, then it's just 00:00. I did this:
public int lives;
public int maxLives;
public Image[] Live;
public Sprite FullHearts;
public Sprite EmptyHearts;
//Timer
[SerializeField] private int minutes;
[SerializeField] private int seconds;
private int m, s;
[SerializeField]
private Text timerText;
private void updateTimer()
{
s--;
if (s < 0)
{
if (m == 0)
{
stopTimer();
return;
}
else {
m --;
s= 59;
}
}
writeTimer(m, s);
Invoke("updateTimer", 1f);
}
public void writeTimer(int m, int s) {
if (s < 10)
{
timerText.text = m.ToString() + ":0" + s.ToString();
}
else
{
timerText.text = m.ToString() + ":" + s.ToString();
}
}
public void stopTimer()
{
CancelInvoke();
}
public void StartTimer()
{
m = minutes;
s= seconds;
writeTimer(m, s);
Invoke("updateTimer", 1f);
}
public void setLives()
{
if (lives < maxLives)
{
StartTimer();
lives++;
Debug.Log("Life: " + lives);
}
if (lives > maxLives)
{
lives = maxLives;
}
for (int i = 0; i < Live.Length; i++)
{
if (i < Mathf.RoundToInt(lives))
{
Live[i].sprite = FullHearts;
}
else
{
Live[i].sprite = EmptyHearts;
}
if(i< maxLives)
{
Live[i].enabled = true;
}
else
{
Live[i].enabled = false;
}
}
}
public void TakeHit(int damage)
{
lives -= damage;
if (lives < 1)
{
lives = 0;
}
for (int i = 0; i < Live.Length; i++)
{
if (i < Mathf.RoundToInt(lives))
{
Live[i].sprite = FullHearts;
}
else
{
Live[i].sprite = EmptyHearts;
}
}
}
but for some reason it does not start, please tell me what the error is possible?