My countdown timer doesn't work,I'm trying to create a countdown timer
I have tried countless time to create a countdown timer form my game, which when reaching zero runs the restart scene. The countdown timer never shows in game or scene view and not sure where I am going wrong. I have place the countdown code into my game manager script and even tried create a new script for this function and pulling as reference in game manager which also didn't work. Not sure what I am doing wrong here, and I have also tried several variations of creating a countdown timer. Any help is greatly appreciate thanks.
I also create an update score method which works perfectly and the score updates in the game, however the countdown timer doesn't show anything.
using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; using UnityEngine.SceneManagement; using UnityEngine.UI;
public class GameManagerX : MonoBehaviour { public TextMeshProUGUI scoreText; public TextMeshProUGUI gameOverText; public TextMeshProUGUI timeText; public GameObject titleScreen; public Button restartButton;
// FIND A WAY TO PULL SCRIPT FROM TIMEX SCRIPT AND ATTACH TO GAME MANAGAER
//TIME
public bool timerIsRunning = false;
public float timeRemaining = 60;
public List<GameObject> targetPrefabs;
private int score;
private float spawnRate = 1.0f;
public bool isGameActive;
private float spaceBetweenSquares = 2.5f;
private float minValueX = -3.75f; // x value of the center of the left-most square
private float minValueY = -3.75f; // y value of the center of the bottom-most square
// Start the game, remove title screen, reset score, and adjust spawnRate based on difficulty button clicked
public void StartGame()
{
timerIsRunning = true;
spawnRate /= 5;
isGameActive = true;
StartCoroutine(SpawnTarget());
score = 0;
UpdateScore(0);
titleScreen.SetActive(false);
}
private void Update()
{
if (timerIsRunning)
{
if (timeRemaining > 0)
{
timeRemaining -= Time.deltaTime;
DisplayTime(timeRemaining);
}
else
{
Debug.Log("Time has run out");
timeRemaining = 0;
timerIsRunning = false;
}
}
void DisplayTime(float timeToDisplay)
{
float minutes = Mathf.FloorToInt(timeToDisplay / 60);
float seconds = Mathf.FloorToInt(timeToDisplay % 60);
timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}
}
// While game is active spawn a random target
IEnumerator SpawnTarget()
{
while (isGameActive)
{
yield return new WaitForSeconds(spawnRate);
int index = Random.Range(0, targetPrefabs.Count);
if (isGameActive)
{
Instantiate(targetPrefabs[index], RandomSpawnPosition(), targetPrefabs[index].transform.rotation);
}
}
}
// Generate a random spawn position based on a random index from 0 to 3
Vector3 RandomSpawnPosition()
{
float spawnPosX = minValueX + (RandomSquareIndex() * spaceBetweenSquares);
float spawnPosY = minValueY + (RandomSquareIndex() * spaceBetweenSquares);
Vector3 spawnPosition = new Vector3(spawnPosX, spawnPosY, 0);
return spawnPosition;
}
// Generates random square index from 0 to 3, which determines which square the target will appear in
int RandomSquareIndex()
{
return Random.Range(0, 4);
}
// Update score with value from target clicked
public void UpdateScore(int scoreToAdd)
{
score += scoreToAdd;
scoreText.text = "score" + score;
}
// Stop game, bring up game over text and restart button
public void GameOver()
{
gameOverText.gameObject.SetActive(true);
restartButton.gameObject.SetActive(true);
isGameActive = false;
}
// Restart game by reloading the scene
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void StartGame(int difficulty)
{
isGameActive = true;
score = 0;
spawnRate /= difficulty;
StartCoroutine(SpawnTarget());
UpdateScore(0);
titleScreen.gameObject.SetActive(false);
}
}
,I have made a score update which displays correctly. However I am having trouble doing the same for the countdown timer. It never shows in my scene and I have tried many different variations of could. Could someone please help indicate where I have gone wrong. The timer should countdown from 60 when ended the restart scene should run. I have placed the timer in the game manager script. I create a new script and pull reference, that also didn't work.
using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; using UnityEngine.SceneManagement; using UnityEngine.UI;
public class GameManagerX : MonoBehaviour { public TextMeshProUGUI scoreText; public TextMeshProUGUI gameOverText; public TextMeshProUGUI timeText; public GameObject titleScreen; public Button restartButton;
// FIND A WAY TO PULL SCRIPT FROM TIMEX SCRIPT AND ATTACH TO GAME MANAGAER
//TIME
public bool timerIsRunning = false;
public float timeRemaining = 60;
public List<GameObject> targetPrefabs;
private int score;
private float spawnRate = 1.0f;
public bool isGameActive;
private float spaceBetweenSquares = 2.5f;
private float minValueX = -3.75f; // x value of the center of the left-most square
private float minValueY = -3.75f; // y value of the center of the bottom-most square
// Start the game, remove title screen, reset score, and adjust spawnRate based on difficulty button clicked
public void StartGame()
{
timerIsRunning = true;
spawnRate /= 5;
isGameActive = true;
StartCoroutine(SpawnTarget());
score = 0;
UpdateScore(0);
titleScreen.SetActive(false);
}
private void Update()
{
if (timerIsRunning)
{
if (timeRemaining > 0)
{
timeRemaining -= Time.deltaTime;
DisplayTime(timeRemaining);
}
else
{
Debug.Log("Time has run out");
timeRemaining = 0;
timerIsRunning = false;
}
}
void DisplayTime(float timeToDisplay)
{
float minutes = Mathf.FloorToInt(timeToDisplay / 60);
float seconds = Mathf.FloorToInt(timeToDisplay % 60);
timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}
}
// While game is active spawn a random target
IEnumerator SpawnTarget()
{
while (isGameActive)
{
yield return new WaitForSeconds(spawnRate);
int index = Random.Range(0, targetPrefabs.Count);
if (isGameActive)
{
Instantiate(targetPrefabs[index], RandomSpawnPosition(), targetPrefabs[index].transform.rotation);
}
}
}
// Generate a random spawn position based on a random index from 0 to 3
Vector3 RandomSpawnPosition()
{
float spawnPosX = minValueX + (RandomSquareIndex() * spaceBetweenSquares);
float spawnPosY = minValueY + (RandomSquareIndex() * spaceBetweenSquares);
Vector3 spawnPosition = new Vector3(spawnPosX, spawnPosY, 0);
return spawnPosition;
}
// Generates random square index from 0 to 3, which determines which square the target will appear in
int RandomSquareIndex()
{
return Random.Range(0, 4);
}
// Update score with value from target clicked
public void UpdateScore(int scoreToAdd)
{
score += scoreToAdd;
scoreText.text = "score" + score;
}
// Stop game, bring up game over text and restart button
public void GameOver()
{
gameOverText.gameObject.SetActive(true);
restartButton.gameObject.SetActive(true);
isGameActive = false;
}
// Restart game by reloading the scene
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void StartGame(int difficulty)
{
isGameActive = true;
score = 0;
spawnRate /= difficulty;
StartCoroutine(SpawnTarget());
UpdateScore(0);
titleScreen.gameObject.SetActive(false);
}
}
ermmm it was as simple as turning on time is running in the inspector :(
Your answer
Follow this Question
Related Questions
I need a "loop" timer that disables a action until the timer resets. 0 Answers
Start and Stop Timer Help 1 Answer
How is it possible to verify values and is they are correct activate objects ? 0 Answers
How to get a countdown timer with values entered by the player 0 Answers
Countdown timer... 2 Answers