- Home /
Pause code wont work
hey I'm trying to implement a pause key for the game I'm creating, but for some reason it's not working, I've tried tagging it to the camera , a GUI text and Empty object none of that work. I've also tried different codes and they also won't work.
This is one of the codes I've tried :
#pragma strict
var paused : boolean = false;
var pausedGUI : GUITexture;
pausedGUI.enabled = false;
function Update () {
if(Input.GetKeyUp("p")) {
if(paused == true)
paused = false;
}
else {
paused = true;
}
if(paused == true)
{
Time.timeScale = 0.0;
pausedGUI.enabled = true;
}
else{
Time.timeScale = 1.0;
pausedGUI.enabled = false;
}
}
**This is another one**
#pragma strict
var isPaused : boolean = false;
function Update() {
if(Input.GetKeyDown("p")) {
Pause();
}
}
function Pause()
{
if (isPaused == true)
{
Time.timeScale = 1;
isPaused = false;
}
else
{
Time.timeScale = 0;
isPaused = true;
}
}
Am I doing something wrong ?
Answer by Habitablaba · Oct 24, 2014 at 06:20 PM
I cleaned up both versions of your script a bit, but overall they look like they should work just fine. I wonder if you don't have this script on more than one object? If so, you should not do that.
As a personal preference, I like the second version of the script better as it will only check the status of the paused variable when it has the potential to be different (when the player presses 'P'). Whereas the first script will set the time scale every frame.
#pragma strict
var paused : boolean = false;
var pausedGUI : GUITexture;
pausedGUI.enabled = false;
function Update () {
if(Input.GetKeyDown(KeyCode.P)){
paused = !paused;
}
if(paused)
{
Time.timeScale = 0.0;
pausedGUI.enabled = true;
}
else{
Time.timeScale = 1.0;
pausedGUI.enabled = false;
}
}
**This is another one**
#pragma strict
var isPaused : boolean = false;
function Update() {
if(Input.GetKeyDown(KeyCode.P)) {
Pause();
}
}
function Pause()
{
if (isPaused)
{
Time.timeScale = 1;
}
else
{
Time.timeScale = 0;
}
isPaused = !isPaused;
}
yeah you're right ^^ I've attached the code only to the main camera , it didn't work , I've tried attaching it to an empty object as well that didn't work either, I'm starting to think that I should attach it to each object but I know that's not right
Oh hey, here's a question: where are you handling the movement of your other objects? Setting the timescale to 0 will not prevent Update() from being called. If you have movement code in Update, that will still get executed. Try moving your movement code to FixedUpdate and see what that does for you.
for more, and how to make it work in Update ins$$anonymous$$d of FixedUpdate (which you don't need if you aren't doing physics) check out the accepted answer here.
Basically, if you aren't scaling your movement by delta time, you should be, then you can move the code back to Update from FixedUpdate.
Don't forget to swing back around to let us know if it worked, and to mark the question as answered.
Answer by 8Eye · Oct 24, 2014 at 04:41 PM
This is how to do it in c#. It will be easy for you to port to JS.
public bool paused = false;
if(Input.GetKeyUp(KeyCode.P)){
paused = !paused //toggles between paused and not paused
}
if(paused){
Time.timeScale = 0;
}
else { Time.timeScale = 1; }
Still not working :( the objects in the scene are still moving
when I enter the game scene and press play this is the only thing that's changing , but the objects in the scene itself are still moving
Your answer
![](https://koobas.hobune.stream/wayback/20220613165846im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Door Open With Object Pickup 1 Answer
How to pause game. 3 Answers
Anyway to get what key is down? 1 Answer
Create GUIText by pressing key 1 Answer
How to Rotate Game Object in specific Angle repeatedly on Key Press? 1 Answer