- Home /
Freeze game using timer from start, then resume play when click the button. It is possible ?
Hello everyone. I'm trying to make the game freeze from start using time. Show the information for player to read then player can click button "Ok" to start the game. But game still freeze after button "Ok" is pressed. Thanks for anyone who help me. Here is my code.
using UnityEngine;
using System.Collections;
public class MissionPause : MonoBehaviour
{
public Canvas missionCanvas;
bool missionPanel = true;
public float Timer = 3f;
void Start ()
{
Timer = Time.time + Timer;
}
void Update ()
{
if (Timer < Time.time)
{
Time.timeScale = 0;
}
}
public void ResumeGame()
{
if (missionPanel == true)
{
missionPanel = false;
missionCanvas.enabled = false;
Time.timeScale = 1 ;
}
}
}
Answer by allenallenallen · Aug 11, 2015 at 01:53 AM
ResumeGame isn't even being called.
And in your Update, Timer < Time.time should be Timer > Time.time. Otherwise, it would continue to freeze forever.
Thank you for faster reply. But I already done it. The game still freeze after i press button "Ok". Any other idea ?
Oh shoot, I forgot about timeScale. You see, if timeScale = 0, then void Update won't be called anymore.
I think it's best to use this:
http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.Invoke.html
public float timer = 3f;
void Start(){
Invoke("UnPause", timer);
Time.timeScale = 0;
}
Hehe, yep!
Time.time is scaled by Time.timeScale, so if the timeScale is 0, you'll never see three seconds pass. On the other hand, there are still other options available in Time, such as Time.unscaledTime or Time.realTimeSinceStartup (where no $$anonymous$$imum framerate will be enforced).
Thank you everyone for great tips. I get my solution. Here is my new code.
using UnityEngine;
using System.Collections;
public class $$anonymous$$issionPause : $$anonymous$$onoBehaviour
{
public Canvas missionCanvas;
bool missionPanel = true;
public float timer = 3f;
void Start ()
{
Invoke ("PauseGame",timer);
}
void PauseGame()
{
Time.timeScale = 0 ;
}
public void ResumeGame()
{
if (missionPanel == true)
{
missionPanel = false;
missionCanvas.enabled = false;
Time.timeScale = 1 ;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220611235302im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Pause gameobjects with tag? 2 Answers
Lots of !IsFinite() Errors and FPS crawling to a halt - How to debug? 1 Answer
Why?? MissingComponentException: 2 Answers
Code error Javascript GUI text 0 Answers
Ontriggerenter not working 1 Answer