Hello there!I recently downloaded the unity ui asset from the asset store and it comes with an in-game pause menu which opens in game but it does not stop the game completely.I have a fps character in my 3d game and when i press ESC and the menu opens,i can still move my camera around and i can’t find where the issue is coming from in the pause menu script.Below is the script from the asset store and i hope you can help me identify the issue :
using UnityEngine;
using System.Collections;
public class Pause : MonoBehaviour {
private ShowPanels showPanels; //Reference to the ShowPanels script used to hide and show UI panels
private bool isPaused; //Boolean to check if the game is paused or not
private StartOptions startScript; //Reference to the StartButton script
//Awake is called before Start()
void Awake()
{
//Get a component reference to ShowPanels attached to this object, store in showPanels variable
showPanels = GetComponent<ShowPanels> ();
//Get a component reference to StartButton attached to this object, store in startScript variable
startScript = GetComponent<StartOptions> ();
}
// Update is called once per frame
void Update () {
//Check if the Cancel button in Input Manager is down this frame (default is Escape key) and that game is not paused, and that we're not in main menu
if (Input.GetButtonDown ("Cancel") && !isPaused && !startScript.inMainMenu)
{
//Call the DoPause function to pause the game
DoPause();
}
//If the button is pressed and the game is paused and not in main menu
else if (Input.GetButtonDown ("Cancel") && isPaused && !startScript.inMainMenu)
{
//Call the UnPause function to unpause the game
UnPause ();
}
}
public void DoPause()
{
//Set isPaused to true
isPaused = true;
//Set time.timescale to 0, this will cause animations and physics to stop updating
Time.timeScale = 0;
//call the ShowPausePanel function of the ShowPanels script
showPanels.ShowPausePanel ();
}
public void UnPause()
{
//Set isPaused to false
isPaused = false;
//Set time.timescale to 1, this will cause animations and physics to continue updating at regular speed
Time.timeScale = 1;
//call the HidePausePanel function of the ShowPanels script
showPanels.HidePausePanel ();
}
}
Yes. If the pause panel appears, this means it also set Time.timeScale to 0. I’m very familiar with these scripts, since they form the foundation of the free Dialogue System Menu Template on the Dialogue System’s free extras page.
Try adding this script to an empty GameObject in your scene: (It’s a variation of IzzySoft’s.)
ShowTimeScale
using UnityEngine;
public class ShowTimeScale : MonoBehavior {
void OnGUI() {
GUILayout.Label("Time.timeScale: " + Time.timeScale);
}
}
Then play the scene. In the upper left, you should see “Time.timeScale: 1”.
When you press Escape (or whatever you’ve mapped to the “Cancel” input in Edit > Project Settings > Input), it should change to “Time.timeScale: 0”.
If your player can’t move while paused but you can still move the camera around, this means the camera script probably isn’t taking Time.timeScale into account. If this is the case, you could add these lines to the top of Pause.cs:
using UnityEngine;
using UnityEngine.Events;
using System.Collections;
public class Pause : MonoBehaviour {
public UnityEvent onPause = new UnityEvent();
public UnityEvent onUnpause = new UnityEvent();
...
At the end of the DoPause() method, add this line:
onPause.Invoke();
At the end of the UnPause() method, add this line:
onUnpause.Invoke();
Then inspect the Pause component of the UI GameObject. It should now have event blocks for “On Pause” and “On Unpause”. Click the “+” in the lower right of each one to create a new slot. Assign your camera script. In the “On Pause” block, select the “enabled” property and keep the checkbox unticked. In the “On Unpause” block, do the same but tick the checkbox.
You can also enable and disable other components this way, too.
When you press the “Cancel” input button (e.g., Escape key), the Pause script calls the DoPause() method.
This method sets Time.timeScale=0 and shows the menu. If the menu doesn’t show up and Time.timeScale doesn’t change to 0, then this method probably isn’t being called. This means the Update() method probably isn’t detecting that the “Cancel” input button is down.
Are you getting errors in the Console?
Check the “Cancel” input definition (Edit > Project Settings > Input) and find out what key it’s mapped to. Then play the game and try pressing that key.
Ok right now i can press Escape and the menu shows up and time.timescale is 0 but i can still move the camera so i guess the fps camera ignores time.timescale?
Sure sounds like it. If you add the extra code in my previous post, you can use the events to disable the camera script when the pause menu opens and re-enable it when the pause menu closes.
The UnityEvent part is giving me an error that that namespace could not be found!
console : Assets/Game Jam Menu Template/Scripts/Pause.cs(6,47): error CS1526: A new expression requires () or [ ] after type
I tried addign () or [ ] after …= new UnityEvent; but nothing changed
I had already added that line at the top of the script by the time you replied by i in your previous post you said : " Then inspect the Pause component of the UI GameObject. It should now have event blocks for “On Pause” and “On Unpause”. Click the “+” in the lower right of each one to create a new slot. Assign your camera script. In the “On Pause” block, select the “enabled” property and keep the checkbox unticked. In the “On Unpause” block, do the same but tick the checkbox. "
however there is no enabled property or checkbox anywhere in those two boxes from what i saw!
When you click “+”, this should add a slot where you can assign a GameObject. It’s similar to the Unity UI Button’s On Click event shown below, but it will say On Pause instead:
If you’re familiar with Unity UI, you should have an idea of how to assign a GameObject to the slot that was created by clicking “+”. This will add a dropdown next to the slot. You can select the camera script and then enabled from the dropdown.
If you assign the camera script’s GameObject to the slot, can you select anything else from the dropdown, such as GameObject > SetActive? (Don’t actually select SetActive, since this will deactivate the GameObject. I’m just curious if you can select anything at all from the dropdown.)
using UnityEngine;
using System.Collections;
using UnityEngine.Events;
public class Pause : MonoBehaviour {
// my code
public UnityEvent onPause = new UnityEvent();
public UnityEvent onUnPause = new UnityEvent();
private ShowPanels showPanels; //Reference to the ShowPanels script used to hide and show UI panels
private bool isPaused; //Boolean to check if the game is paused or not
private StartOptions startScript; //Reference to the StartButton script
//Awake is called before Start()
void Awake()
{
//Get a component reference to ShowPanels attached to this object, store in showPanels variable
showPanels = GetComponent<ShowPanels> ();
//Get a component reference to StartButton attached to this object, store in startScript variable
startScript = GetComponent<StartOptions> ();
}
// Update is called once per frame
void Update () {
//Check if the Cancel button in Input Manager is down this frame (default is Escape key) and that game is not paused, and that we're not in main menu
if (Input.GetButtonDown ("Cancel") && !isPaused && !startScript.inMainMenu)
{
//Call the DoPause function to pause the game
DoPause();
}
//If the button is pressed and the game is paused and not in main menu
else if (Input.GetButtonDown ("Cancel") && isPaused && !startScript.inMainMenu)
{
//Call the UnPause function to unpause the game
UnPause ();
}
if (startScript.inMainMenu)
{
Debug.Log("Time.timeScale: " + Time.timeScale);
}
}
public void DoPause()
{
//Set isPaused to true
isPaused = true;
//Set time.timescale to 0, this will cause animations and physics to stop updating
Time.timeScale = 0;
//call the ShowPausePanel function of the ShowPanels script
showPanels.ShowPausePanel ();
//my code
onPause.Invoke();
}
public void UnPause()
{
//Set isPaused to false
isPaused = false;
//Set time.timescale to 1, this will cause animations and physics to continue updating at regular speed
Time.timeScale = 1;
//call the HidePausePanel function of the ShowPanels script
showPanels.HidePausePanel ();
//my code
onUnPause.Invoke();
}
}
I pasted that script verbatim, and it seems to work:
Since I don’t have a copy of your camera control script, in the screenshot above I just chose the enabled property of the Camera component on the GameObject named Main Camera.