- Home /
Hello, how do I display in text the amount of time before the cool down ends in this script?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class AttackCoolDown : MonoBehaviour {
public float cooldownTime = 5;
private float nextFireTime = 0;
private void Update()
{
if(Time.time > nextFireTime)
{
if(Input.GetKeyDown(KeyCode.E))
{
Debug.Log("Cooldown has started.");
nextFireTime = Time.time + cooldownTime;
}
}
}
}
Comment
Best Answer
Answer by Hellium · Feb 07, 2021 at 06:47 PM
public UnityEngine.UI.Text cooldownText; // Drag & drop UI text in inspector
private void Update()
{
if(Time.time > nextFireTime)
{
cooldownText.SetActive(false);
if(Input.GetKeyDown(KeyCode.E))
{
Debug.Log("Cooldown has started.");
nextFireTime = Time.time + cooldownTime;
cooldownText.SetActive(true);
}
}
else
{
cooldownText.text = (nextFireTime - Time.time).ToString("F0");
}
}
I'd just use T$$anonymous$$Pro.Text$$anonymous$$eshProUGUI cooldownText instead of UnityEntine.UI.Text, but just a detail.