Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by PerrelliGXi · Feb 01 at 04:06 PM · scripting problemtimercountdowntimer countdowntimer-script

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);
     

 }




}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PerrelliGXi · Feb 01 at 02:32 PM 0
Share

ermmm it was as simple as turning on time is running in the inspector :(

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

277 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need a "loop" timer that disables a action until the timer resets. 0 Answers

Allow player to Press Input.GetKeyDown once per update ? 0 Answers

How to make a depleting bar timer GUI please help! 0 Answers

how to save daily reward timer by playerprefs ? 0 Answers

Change the Intensity of a Haptic Feedback with respect to time ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges