Pause Menu
Hi, my pause menu isn't working at all, I've followed someone's code to try and get it working and it just won't work at all, I hit pause and the menu pops up but I can still look about and move the camera around, what is wrong with my code?
I also get this error message "NullReferenceException: Object reference not set to an instance of an object PauseScript.Update () (at Assets/MyFolder/My Scripts/PauseScript.js:27)"
#pragma strict
var pauseCanvas : Canvas;
var isPause = false;
function Start()
{
Screen.lockCursor = true;
Cursor.visible = false;
}
function Update()
{
if( Input.GetKeyDown(KeyCode.Escape))
{
isPause = !isPause;
if(isPause)
{
Time.timeScale = 0;
pauseCanvas.enabled = true;
}
else
{
Time.timeScale = 1;
pauseCanvas.enabled = false;
}
(gameObject.Find("First Person Controller").GetComponent("MouseLook") as MonoBehaviour).enabled = false;
(gameObject.Find("Main Camera").GetComponent("MouseLook") as MonoBehaviour).enabled = false;
pauseCanvas.enabled = true;
Time.timeScale = 0;
Screen.lockCursor = false;
Cursor.visible = true;
}
}
function ResumeGame()
{
}
Answer by TBruce · Nov 03, 2016 at 04:33 PM
The reason why you get an error is because MouseLook
is not a class derived from MonoBehaviour. This will fix you problem
#pragma strict
var pauseCanvas : Canvas;
var isPause = false;
function Start()
{
Screen.lockCursor = true;
Cursor.visible = false;
if (pauseCanvas != null)
{
pauseCanvas.gameObject.SetActive(false);
pauseCanvas.enabled = false;
}
}
function Update()
{
if( Input.GetKeyDown(KeyCode.Escape))
{
isPause = !isPause;
if(isPause)
{
Time.timeScale = 0;
pauseCanvas.enabled = true;
Screen.lockCursor = true;
Cursor.visible = true;
}
else
{
Time.timeScale = 1;
pauseCanvas.enabled = false;
Screen.lockCursor = false;
Cursor.visible = true;
}
if (pauseCanvas != null)
{
if (GameObject.Find("First Person Controller") != null)
{
GameObject.Find("First Person Controller").SetActive(!isPause);
}
pauseCanvas.enabled = isPause;
pauseCanvas.gameObject.SetActive(isPause);
}
}
}
function ResumeGame()
{
}
That worked great! Thank you, my camera is still moving around when in the pause state? It shouldn't do that?
Do you think you can help me with something else?
http://answers.unity3d.com/questions/1266620/trigger-a-timer.html
Your original code had this
Cursor.visible = false;
in the start menu. I just went with it. Here is an update to the script
#pragma strict
var pauseCanvas : Canvas;
var isPause = false;
var player : GameObject;
function Start()
{
Screen.lockCursor = false;
Cursor.visible = true;
if (pauseCanvas != null)
{
pauseCanvas.gameObject.SetActive(false);
pauseCanvas.enabled = false;
}
if (player == null)
{
if (GameObject.Find("First Person Controller") != null)
{
player = GameObject.Find("First Person Controller");
}
else if (GameObject.FindWithTag("Player") != null)
{
player = GameObject.FindWithTag("Player");
}
}
}
function Update()
{
if( Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))
{
isPause = !isPause;
if(isPause)
{
Time.timeScale = 0;
pauseCanvas.enabled = true;
Screen.lockCursor = false;
Cursor.visible = true;
}
else
{
Time.timeScale = 1;
pauseCanvas.enabled = false;
Screen.lockCursor = false;
Cursor.visible = true;
}
if (pauseCanvas != null)
{
if (player != null)
{
player.SetActive(!isPause);
}
pauseCanvas.enabled = isPause;
pauseCanvas.gameObject.SetActive(isPause);
}
}
}
function ResumeGame()
{
}
You should really place the script on a separate game object and definitely not on the FPS controller. Also, when you create the FPS controller using the prefab, the default name of the object is FPSController
, but in your code you seem to have changed it to First Person Controller
. You need to make sure that the name is correct both in the hierarchy and the script.
Also, you can set the tag of your player to Player
(if not already done so and use GameObject.FindWithTag()
ins$$anonymous$$d). For efficiency I created a player object variable and placed it as a global and set it in the Start()
function ins$$anonymous$$d of contiually doing it.
Okay That all works fine, how can I keep a camera on when the game is paused?
It just brings up my canvas and doesn't let me move the mouse around just spins the camera around.