I can't pause my game? How to make a pause menu in C# for SPace Shooter type Game?

I searched on Youtube and Unity Answers about the pause menu but the only problem is that the game objects of the game don’t stop and continue if it paused. I also am trying to create a pause menu once the user clicks on pause button. But, i am enable to do it. Following is my code:

using UnityEngine;
using System.Collections;

public class PauseGame : MonoBehaviour {

public GameObject Gameobject=null;
public GUISkin layer;
private Rect windowRect;
private bool paused = false , waited = true;

private void Start()
{
	windowRect = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 500, 500);
}

private void waiting()
{
	if (waited)
			if (Input.GetMouseButtonDown(0) || Input.GetKey (KeyCode.Escape)) {
				
				if(paused)
					paused = false;
				else
					paused = true;
		waited = false;
		Invoke("waiting",0.3f);
	}
}

private void OnGUI ()
{
	if (paused)
		windowRect = GUI.Window (0, windowRect, windowFunc, "Game Paused");
}
private void windowFunc(int id)
{
	if (GUILayout.Button("Resume Game")){
		paused = false;		
	}
	if (GUILayout.Button("Restart")) {
		Application.LoadLevel(4);
	}
	if (GUILayout.Button("Main Menu")) {
		Application.LoadLevel(1);
	}
	if (GUILayout.Button("Exit Game")) {
		Application.Quit();
	}
}

}

2 Answers

2

One way to do this in C# is when the game is paused, set Time.timeScale to 0.0f and when it’s unpaused reset it to 1.0f… and in all the scripts that you want to pause, instead of using Update() use FixedUpdate()

I know it’s a bit of a faff but it worked for me about a week ago when I had the same problem :slight_smile:

Thanks, I would definitely try this

how about something like this:

void OnGUI(){

if (GUI.Button (new Rect (Screen.width - 115, 90, 100, 50), pauseText.text)) {

					if (!paused) {

							Time.timeScale = 0;
							paused = true;
							pauseText.text = "Play";

                               // do whatever you want while paused

					}

else {

							Time.timeScale = 1.0f;
							pauseText.text = "Pause";
							paused = false;
							//audio.Play ();
					}
			}

Thanks, I would try this out