Hello
So I want to create a GAME OVER word that will be displayed on my screen when the timer hits 0. I tried to create a text component as well as declaring it in my script below but doesn’t seems to work. I also untick the text component so that it doesn’t appear when I start the game, but when the Timer hits 0, nothing appear. So please send help if you can and thank you for your time. Here’s my Timer script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour
{
public float timeLeft = 20;
public bool timeIsRunning = false;
public Text timeText;
public Text gameOverText;
public Color myColor;
// Start is called before the first frame update
private void Start()
{
//Automatically starts the timer
timeIsRunning = true;
}
// Update is called once per frame
void Update()
{
if (timeIsRunning)
{
if (timeLeft > 0)
{
timeLeft -= Time.deltaTime;
}
else
{
Debug.Log("Out of time!");
timeLeft = 0;
timeIsRunning = false;
}
DisplayTime(timeLeft);
}
void DisplayTime(float timeToDisplay)
{
if (timeToDisplay < 0)
{
timeToDisplay = 0;
}
else if (timeToDisplay <= 6)
{
timeText.color = myColor;
}
else if (timeToDisplay == 0)
{
gameOverText.text = "Game Over";
}
float minutes = Mathf.FloorToInt(timeToDisplay / 60);
float seconds = Mathf.FloorToInt(timeToDisplay % 60);
timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}
}
}