- Home /
 
Game over when time reached
Hello,
I wish to continue a script so that it will stop the time and game, when it reaches 15 mins, it start from 00:00 of game time. This is what I have so far. using UnityEngine.UI; using UnityEngine;
 public class Timer : MonoBehaviour
 {
 
     public Text timerText;
     private float startTime;
     public float t;
     public int points = 0;
     int winTotal = 2;
     public GameManager gameManager;
     private void OnGUI()
     {
         GUI.Label(new Rect(10, 10, 100, 20), "Löydetty : " + points);
     }
 
     void Start()
     {
         startTime = Time.time;
     }
 
     void Update()
     {
         if (points == winTotal)
             return;
         {
             float t = Time.time - startTime;
             string minutes = ((int)t / 59).ToString("00");
             string seconds = (t % 59).ToString("00");
 
             timerText.text = minutes + ":" + seconds;
 
             FindObjectOfType<GameManager>().EndGame();
         }
     }
 }
 
               So yes, time stops when the player has collected the NPC:s. I should be able to work from there again.
Thank you in advance.
Answer by H1ddeNGames · Dec 22, 2018 at 10:58 AM
How about using a Coroutine?
First create a variable private IEnumerator coroutine;
Next set that variable coroutine = Timer();
Start your coroutine using StartCoroutine(coroutine);
Stop your coroutine using StopCoroutine(coroutine);
 IEnumerator Timer() 
 {
 // 900 seconds is 15 minutes.
     yield return new WaitForSeconds(900f);
     gameManager.EndGame();
 }
 
               Also you don't need to use FindObjectOfType<GameManager>() when you have a reference at the top of your script public GameManager gameManager;. Just do gameManager.EndGame();
Thanks for the tip on gamemanager. I see if I get this working and ask if not I'll ask again what to do. Thanks.
You could also simplify to :
  Invoke("EndGame", 900f);
 
                  Your answer
 
             Follow this Question
Related Questions
C# simple delay execution without coroutine? 2 Answers
Distribute terrain in zones 3 Answers
Making a fill take exactly n seconds to complete 2 Answers
Unexpected coroutine behavior for simple timer 2 Answers
Multiple Cars not working 1 Answer