8
8
8_JOHN_82018-12-04 22:52:03
Unity
8_JOHN_8, 2018-12-04 22:52:03

How to implement a pause in a game with a countdown counter?

Hello everyone, there is a question about the pause in the game, it turns out that I made an event on the pause button when the timescale is 0, then I checked when it is pressed again, if it is pressed again during the pause, the time should start and then timescale 1 will be assigned, in as a result, the countdown goes only when I click on the pause itself, and not automatically .... Here is the script ......... Why I don’t check in the update, because as I understand it, because of the time scale which is zero, the update does not work, how can this be done, any suggestions?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class ButtonPause : MonoBehavior
{
public Text time_text;
public int timer;
bool time_pause = false;
public void OnMouseUp()
{
if (Time.timeScale == 1)
{
Time.timeScale = 0;
time_pause = false;
}
else
{
time_pause = true;
StartCoroutine(WaitAndPrint());
}
}
IEnumerator WaitAndPrint()
{
if (time_pause)
{
//Debug.Log("TimerCount: " + (timer--));
if (timer == 3)
{ time_text.text = "3"; timer--; yield return new WaitForSeconds(1f); }
//
if (timer ==2)
{ time_text.text = "2"; timer--; yield return new WaitForSeconds(1f); }
//
if (timer == 1)
{ time_text.text = "1"; timer--; yield return new WaitForSeconds(1f); }
//
if (timer == 0)
{ time_text.text = "GO"; Time.timeScale = 1; timer--; yield return new WaitForSeconds(1f); }
//
if (timer == -1)
{ time_text.text = ""; timer = 3; time_pause = false; }
//yield return new WaitForSeconds(1f);
}
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Griboks, 2018-12-05
@Griboks

Make the timer in a separate thread.

A
Alex Maximovich, 2018-12-05
@flexer1992

You have a rather strange implementation. In fact, you can store the time when our reverse timer should end. When pausing, remember the pause time. When unpausing, add the difference between the moment of setting and unpausing to the end time of the reverse timer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question