60 lines
1.3 KiB
C#
60 lines
1.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using TMPro; // Import TextMeshPro namespace
|
|
|
|
public class Timer : MonoBehaviour
|
|
{
|
|
public TextMeshProUGUI textTimer; // Change to TextMeshProUGUI
|
|
public float waktu = 100; // 01:30
|
|
|
|
public bool GameAktif = true;
|
|
public GameObject GameOver;
|
|
public static Timer Instance { get; private set; }
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
void SetText()
|
|
{
|
|
int menit = Mathf.FloorToInt(waktu / 60); // 01
|
|
int detik = Mathf.FloorToInt(waktu % 60); // 30
|
|
string v = menit.ToString("00") + " :" + detik.ToString("00");
|
|
textTimer.text = v;
|
|
}
|
|
|
|
float s;
|
|
|
|
private void Update()
|
|
{
|
|
if (GameAktif)
|
|
{
|
|
s += Time.deltaTime; // Accumulate time
|
|
if (s >= 1)
|
|
{
|
|
waktu--; // Decrease time
|
|
s = 0; // Reset the accumulator
|
|
}
|
|
}
|
|
|
|
if(GameAktif && waktu <=0)
|
|
{
|
|
Debug.Log("Game Kalah");
|
|
GameOver.SetActive(true);
|
|
GameAktif = false;
|
|
}
|
|
|
|
SetText(); // Update the text display
|
|
}
|
|
}
|