Logo my studio is game, portfólio is developer game

Regressive time count in unity with Coroutine

In this unity tutorial, I'll show you a c# script that makes a regressive time count using Coroutine: 

using UnityEngine;
using System.Collections;
using System;
public class timeCoroutine : MonoBehaviour
{
[SerializeField] byte timeCurrent, timeLimit = 5; // 90 segundos = 1 : 30
[SerializeField] bool timeGame = true;
void Start()
{
timeCurrent = timeLimit;
StartCoroutine(CountTimer());
}
private IEnumerator CountTimer()
{
while (timeGame)
{
yield return new WaitForSeconds(1f);
timeCurrent--;
string minutes = Mathf.Floor(timeCurrent / 60).ToString("00");
string seconds = Mathf.Floor(timeCurrent % 60).ToString("00");
string tempoCurrent = minutes + ":" + seconds;
print(tempoCurrent);
if (timeCurrent == 0)
{
print("tempo acabo");
timeGame = false;
}
}
}
}

Make contact!

Did you like this content? Let's talk...

I'm a game developer, I use unity. If you want to develop something, tell me!