• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AcidZack · Nov 06, 2014 at 06:08 AM · programmingreferencepausemouselock

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ozzyel-PT · Nov 06, 2014 at 06:35 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

help please :S? im confusing myself. 2 Answers

How do I make my scripts more affective? 2 Answers

Filling array with Scribtable Objects automaticly? 1 Answer

When an object is DontDestroyOnLoad(), where can I put script that will run at the start of a new scene? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges