- Home /
Pause Script Suddenly Stops Working
Hello,
I had a simple pause script (adapted from this tutorial) that I built last night. It worked very well and was 100% reliable. Tonight, it seems to have broken, though I've made no changes to the script (or any other script, for that matter).
The script is here:
#pragma strict
private var pauseGame : boolean = false;
private var showGUI : boolean = false;
function Update()
{
if(Input.GetButtonDown("Pause"))
{
pauseGame = !pauseGame;
if(pauseGame == true)
{
Time.timeScale = 0;
pauseGame = true;
(GameObject.Find("Main Camera").GetComponent("MouseLook") as MonoBehaviour).enabled = false;
(GameObject.Find("First Person Controller").GetComponent("MouseLook") as MonoBehaviour).enabled = false;
showGUI = true;
}
}
if(pauseGame == false)
{
Time.timeScale = 1;
pauseGame = false;
(GameObject.Find("Main Camera").GetComponent("MouseLook") as MonoBehaviour).enabled = true;
(GameObject.Find("First Person Controller").GetComponent("MouseLook") as MonoBehaviour).enabled = true;
showGUI = false;
}
if(showGUI == true)
{
(gameObject.Find("PausedText").GetComponent("Text") as MonoBehaviour).enabled = true;
(gameObject.Find("PausedImage").GetComponent("Image") as MonoBehaviour).enabled = true;
(gameObject.Find("Crosshair").GetComponent("Image") as MonoBehaviour).enabled = false;
(GameObject.Find("Main Camera").GetComponent("Blur") as MonoBehaviour).enabled = true;
}
else
{
(gameObject.Find("PausedText").GetComponent("Text") as MonoBehaviour).enabled = false;
(gameObject.Find("PausedImage").GetComponent("Image") as MonoBehaviour).enabled = false;
(gameObject.Find("Crosshair").GetComponent("Image") as MonoBehaviour).enabled = true;
(GameObject.Find("Main Camera").GetComponent("Blur") as MonoBehaviour).enabled = false;
}
}
Now, when I run the game, timeScale goes to 0, but the other functions (disabling MouseLook and toggling the GUI stuff) do not work.
However, if I turn on my PauseGUI by default, it will render (even though it should not per this script) and turn off the first time Escape is tapped, then be unresponsive.
I get this error in the console:
NullReferenceException: Object reference not set to an instance of an object
Pause.Update () (at Assets/Scripts/Pause.js:26)
The only thing that changed was that I downloaded and imported the Survival Shooter demo, played with it for a few minutes, then deleted it when I discovered how badly it mucked up my folder structure. I did not delete any of the files that this one interacts with (Mouselook and blur are still there) nor did I alter the shape of my prefab.
So... does anyone have any thoughts? Cause I'm at a loss. :D
you need to declare variables. You would do it as so:
var g : gameObject;
function Update()
{
g = gameObject.Find("yourGameObjectGoesHere").GetComponent<yourComponentHere>() as $$anonymous$$onoBehaviour.enabled = true; // or false
}
Unfortunately, the code you suggested (and a few permutations) did not solve the issue.
I rewrote the script from scratch, first the tutorial and then my own personal modifications, and it works again. It looks identical; I'm not sure what changed. However, all is well. :) Thanks for your suggestion.