- Home /
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;
}
}
}
}
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.
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?
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.
$$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 :)
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;
}
}
}
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.
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.
Sorry, I misread the issue. Are there any errors that pop up? Cause there doesn't seem to be anything wrong with the code.
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
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();
So, keep the switch with just game paused and use an if somewhere else?
Your answer
![](https://koobas.hobune.stream/wayback/20220613143051im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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