- Home /
Question by
nickking92 · Jan 13, 2018 at 07:14 PM ·
scenetimetimerquit
Quit game after timer reaches 0
i have this code.when timer reaches zero game is closed .How to enable timer working on all scenes ,not only on first?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TImer : MonoBehaviour {
public int timeLeft = 150;
public Text countdownText;
// Use this for initialization
void Start()
{
StartCoroutine("LoseTime");
}
// Update is called once per frame
void Update()
{
countdownText.text = ("Time Left = " + timeLeft);
if (timeLeft <= 0)
{
StopCoroutine("LoseTime");
countdownText.text = "Times Up!";
Application.Quit();
}
}
IEnumerator LoseTime()
{
while (true)
{
yield return new WaitForSeconds(1);
timeLeft--;
}
}
}
Comment
Answer by Grish_tad · Jan 13, 2018 at 08:20 PM
Hi @nickking92 you can use DontDestroyOnLoad function. For more see here https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Your answer
Follow this Question
Related Questions
Help with time.time when new scene starts. 1 Answer
How to work a real life timer? 2 Answers
Help with SpeedRun personal best script 0 Answers
Minute Timer Issue 1 Answer
Time based scoring 2 Answers