So this is the super basic timer I am currently working on. I am using it for a world space UI so.... But hopefully it works for all of you guys.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CountDownTimer : MonoBehaviour {
public Text timerText;
[SerializeField] private float timeLimit = 10f;
private bool activeTimer = true;
private float t;
GameObject player;//reference to the player gameobject (not sure if needed)
PlayerHealth playerHealth;// reference to the health script
private void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent();
}
// Update is called once per frame
void Update() {
if (!activeTimer)
return;
t = timeLimit - Time.time;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
if (t <= 0)
{
TimeRanOut();
}
↧