- Home /
Accessing Variables from other scripts
I'm kind of a programming noob, but my knowledge should be good enough to get this simple game I'm trying to make done.
So my question is that I have a pause menu, a GUI appears and I want to be able to click a button and things will happen.
When you press escape, the game pauses just fine, and when you press escape it un-pauses. I also want to be able to click the continue button to un-pause the game.
I have a script for the continue button that references the "Is paused" paused variable and i want to set it to false, like this:
function OnMouseDown ()
{
GameObject.Find("Main Camera").GetComponent(Pause).pauseGame = false;
}
but it doesn't seem to be working.
Here is the script for my pause interface:
public var pauseGame : boolean = false; //the variable I'm trying to access
public var showGUI : boolean = false;
public var optionsMenu : boolean = false;
function Start()
{
Screen.lockCursor = true;
}
function Update ()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
pauseGame = !pauseGame;
if(pauseGame == true)
{
Time.timeScale = 0;
pauseGame = true;
GameObject.Find("Main Camera").GetComponent (MouseLook).enabled = false;
GameObject.Find("Main Camera").GetComponent (ColorCorrectionCurves).enabled = true;
GameObject.Find("Main Camera").GetComponent (NoiseAndGrain).enabled = true;
GameObject.Find("First Person Controller").GetComponent (MouseLook).enabled = false;
GameObject.Find("Crosshair").GetComponent(GUITexture).enabled = false;
Screen.lockCursor = false;
showGUI = true;
}
else if(pauseGame == false)
{
Time.timeScale = 1;
pauseGame = false;
Screen.lockCursor = true; //Not relocking!
GameObject.Find("Main Camera").GetComponent (MouseLook).enabled = true;
GameObject.Find("Main Camera").GetComponent (ColorCorrectionCurves).enabled = false;
GameObject.Find("Main Camera").GetComponent (NoiseAndGrain).enabled = false;
GameObject.Find("First Person Controller").GetComponent (MouseLook).enabled = true;
GameObject.Find("Crosshair").GetComponent(GUITexture).enabled = true;
showGUI = false;
}
}
}
If anyone can help me that would be wonderful!
ALSO: I'm having trouble with my mouse lock command. The mouse unlocks just fine when I go to pause, but it doesn't relock when I un-pause the game, and I need it to go back to being hidden and locked.
The code to pause/unpase is inside the Input.Get$$anonymous$$eyDown Condition... so I suppose you can't access all that code because that.. try using coroutines or funtions in java I'm not fammiliar to Java.
Answer by b1gry4n · Nov 06, 2014 at 06:49 AM
You could try setting it up as a function...
function Update ()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
pauseGame = !pauseGame;
PauseGame(pauseGame);
}
}
public function PauseGame(should : boolean)
{
if(should){
Time.timeScale = 0;
pauseGame = true; // this is not needed as weve already stated pauseGame was true under Input.GetKeyDown
GameObject.Find("Main Camera").GetComponent (MouseLook).enabled = false;
GameObject.Find("Main Camera").GetComponent (ColorCorrectionCurves).enabled = true;
GameObject.Find("Main Camera").GetComponent (NoiseAndGrain).enabled = true;
GameObject.Find("First Person Controller").GetComponent (MouseLook).enabled = false;
GameObject.Find("Crosshair").GetComponent(GUITexture).enabled = false;
Screen.lockCursor = false;
showGUI = true;
}else{
Time.timeScale = 1;
pauseGame = false; // this is not needed as weve already stated pauseGame was false under Input.GetKeyDown
Screen.lockCursor = true;
GameObject.Find("Main Camera").GetComponent (MouseLook).enabled = true;
GameObject.Find("Main Camera").GetComponent (ColorCorrectionCurves).enabled = false;
GameObject.Find("Main Camera").GetComponent (NoiseAndGrain).enabled = false;
GameObject.Find("First Person Controller").GetComponent (MouseLook).enabled = true;
GameObject.Find("Crosshair").GetComponent(GUITexture).enabled = true;
showGUI = false;
}
}
And your other script could be something like...
pauseScript.PauseGame(false); // pauseScript is a reference to the above script. you will need to set that up
ALso, why not set up a variable for your main camera so you dont have to always "Find" it. Find is expensive, especially because you are searching by name.
I'm not familiar with javascript but I think everything above is correct
I found a great thread that may help you out : http://forum.unity3d.com/threads/newbie-guide-to-unity-javascript-long.34015/
As for the screen locking goes, I am not sure. I have never tried to lock the mouse to the screen. I would start: http://docs.unity3d.com/ScriptReference/Screen-lockCursor.html
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
help please :S? im confusing myself. 2 Answers
Timescale wont work 0 Answers
Listen to audio when stepping frame by frame in editor 1 Answer
How to pause game when receiving phone call on Android 6? 2 Answers