Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Kk1496 · Apr 19, 2014 at 05:59 AM · c#inventorypausesync

How to synchronize pause and inventory scripts

I made a pause script that locks the camera and character controls. I've also begun working on an inventory script. I want the player to be able to use the mouse when the inventory is open so i did the same thing as the pause script. However, this only works once, if pause or inventory is opened more than once, the camera doesn't lock any more. How can I fix this or, would it be wiser to make the inventory part of the pause screen? How would I go about doing that?

Inventory

 void Update(){
         if(Input.GetButtonDown("Inventory")){
             if(showInventory){
                 Time.timeScale = 1; //DO NOT CHANGE VALUE
                 player.GetComponent<MouseLook>().enabled = true;
                 player.GetComponent<CharacterController>().enabled = true;
                 mainCamera.GetComponent<MouseLook>().enabled = true;
                 showInventory = false;
             }
             else if(!showInventory){
                 Time.timeScale = 0; //DO NOT CHANGE VALUE
                 player.GetComponent<MouseLook>().enabled = false;
                 player.GetComponent<CharacterController>().enabled = false;
                 mainCamera.GetComponent<MouseLook>().enabled = false;
                 showInventory = true;
             }
         }
     }

Pause

 public class Pause : MonoBehaviour {
     public static bool gamePaused = false;
 
     public GameObject player;
     public GameObject mainCamera;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         GamePause();    
     }
 
     void OnGUI(){
         if(gamePaused){
             GUI.Box(new Rect((Screen.width / 2), (Screen.height /2), 100, 50), "Pause");
         }
     }
 
     public void GamePause(){
         if(Input.GetButtonDown("Pause")){
             if(gamePaused){
                 Time.timeScale = 1; //DO NOT CHANGE VALUE
                 player.GetComponent<MouseLook>().enabled = true;
                 player.GetComponent<CharacterController>().enabled = true;
                 mainCamera.GetComponent<MouseLook>().enabled = true;
                 gamePaused = false;
             }
             else if(!gamePaused){
                 Time.timeScale = 0; //DO NOT CHANGE VALUE
                 player.GetComponent<MouseLook>().enabled = false;
                 player.GetComponent<CharacterController>().enabled = false;
                 mainCamera.GetComponent<MouseLook>().enabled = false;
                 gamePaused = true;
             }
         }
     }
 
 }
 
Comment
Add comment · Show 4
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 NickP_2 · Apr 19, 2014 at 06:28 AM 0
Share

Why else if? If im not wrong, the if function with showinventory will execute, then set showinventory false and execute the else if afterwards? Id rather use if/else because a menu has 2 states, and with if/ else if/ else you can create unexpexted things. A switch statement would also be better. Really correct me if im wrong but i would get rid of the else if statements in your case.

avatar image Kk1496 · Apr 19, 2014 at 10:42 PM 0
Share

I tried this:

 switch(gamePaused){
             case 1:
                 Time.timeScale = 1; //DO NOT CHANGE VALUE
                 player.GetComponent<$$anonymous$$ouseLook>().enabled = true;
                 player.GetComponent<CharacterController>().enabled = true;
                 mainCamera.GetComponent<$$anonymous$$ouseLook>().enabled = true;
                 gamePaused = false;
                 break;
             case 2:
                 Time.timeScale = 0; //DO NOT CHANGE VALUE
                 player.GetComponent<$$anonymous$$ouseLook>().enabled = false;
                 player.GetComponent<CharacterController>().enabled = false;
                 mainCamera.GetComponent<$$anonymous$$ouseLook>().enabled = false;
                 gamePaused = true;
                 break;
             }

Am I doing something wrong?

avatar image NoseKills · Apr 20, 2014 at 01:06 AM 0
Share

What you did with the "if else if" is not wrong. The latter "if" is just redundant, unnecessary and always returns true :) If you write the optional curly brackets to outline the scope of the else branch, it's easier to see.

 if (showInventory)
 {
   // condition was true
 }
 else
 {
   // condition was false
   if (!showInventory)  // this is unnecessary since "condition" is always false in the else-branch anyways
   {
   }
 }

As for the problem, I can't see much wrong with the little code you posted so I think the problem lies somewhere else or is an indirect symptom of what you're doing here. I'd recommend a little debugging :)

You could start by putting calls like Debug.Log("gamePaused: " + gamePaused) inside the if(Input.GetButtonDown("Pause")) and if(Input.GetButtonDown("Inventory")) and looking at the console to see if the booleans are getting set and key presses are getting triggered as supposed.

P.S. the Switch-case variable gamePaused is a boolean, so it should be:

 switch (gamePaused)
             {
                 case true:
                     Debug.Log("gamePaused was true");
                 break;
                 
                 case false:
                     Debug.Log("gamePaused was false");
                 break;
             }
 

I personally think if-else is always a better solution if the only variable being checked is a boolean. It can only have 2 values anyways.

avatar image NickP_2 · Apr 21, 2014 at 02:39 PM 0
Share

$$anonymous$$y bad here, and I forgot to mention, I also use enums to switch between menu states. Easy to use, and makes code also more readable :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Kk1496 · Apr 20, 2014 at 07:33 AM

This might not be the most efficient way. but, it works.

 public void GamePause(){
         if(Input.GetButtonDown("Pause")){
             if(gamePaused){
                 Time.timeScale = 1; //DO NOT CHANGE VALUE
                 player.GetComponent<MouseLook>().enabled = true;
                 player.GetComponent<CharacterController>().enabled = true;
                 mainCamera.GetComponent<MouseLook>().enabled = true;
                 gamePaused = false;
             }
             else{
                 Time.timeScale = 0; //DO NOT CHANGE VALUE
                 player.GetComponent<MouseLook>().enabled = false;
                 player.GetComponent<CharacterController>().enabled = false;
                 mainCamera.GetComponent<MouseLook>().enabled = false;
                 gamePaused = true;
             }
 
             if(!gamePaused && Inventory.showInventory){
                 Time.timeScale = 0; //DO NOT CHANGE VALUE
                 player.GetComponent<MouseLook>().enabled = false;
                 player.GetComponent<CharacterController>().enabled = false;
                 mainCamera.GetComponent<MouseLook>().enabled = false;
             }
         }
     }
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
avatar image
0

Answer by Magiichan · Apr 20, 2014 at 01:05 AM

Is the pause linked to the escape button? cause that automatically shows your cursor in the unity editor. try building the game and see if you still have that issue.

Comment
Add comment · Show 6 · 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
avatar image Kk1496 · Apr 20, 2014 at 01:28 AM 0
Share

I'm not worried about the cursor showing up. I have an FPS controller on so the camera moves. But, if your trying to say that keeping it on escape is a bad idea, i'll change it.

avatar image Magiichan · Apr 20, 2014 at 01:40 AM 0
Share

Sorry, I misread the issue. Are there any errors that pop up? Cause there doesn't seem to be anything wrong with the code.

avatar image Kk1496 · Apr 20, 2014 at 01:51 AM 0
Share

I've found that my problem is that when you pause the game from the inventory menu and play again, it doesn't continue to lock the camera.

I tried to test two variables by saying:

inventory.cs public static bool showInventory;

puse.cs switch (gamePused && inventory.showInventory)

and now it says

Assets/Scripts/Utilities/Pause.cs(30,56): error CS0122: `Inventory.showInventory' is inaccessible due to its protection level

avatar image Magiichan · Apr 20, 2014 at 03:14 AM 0
Share

The error shows that the inventory class has a capital I, so it should be Inventory.showInventory, also you can't switch multiple variables, so you should just do Something like: if(showInventory) blabla(); else blablabla();

avatar image Kk1496 · Apr 20, 2014 at 03:24 AM 0
Share

So, keep the switch with just game paused and use an if somewhere else?

Show more comments

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.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Change variable on all players with UNet 1 Answer

Pause movement on collision 2 Answers

Make enemy wait before attacking player 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