using Assets.Scripts.Managers;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public static bool Paused = false;
public GameObject pauseMenuUI;
private PlayerInputs pInputs;
private InputAction pause;
private GameObject[] players;
private void Awake()
{
pInputs = new PlayerInputs();
}
public void Resume()
{
players = GameObject.FindGameObjectsWithTag("PlayerOb");
foreach (GameObject player in players)
player.GetComponent<InputHandler>().SetPlayerActionsEnabled(true);
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
AudioListener.pause = false;
Paused = false;
}
public void Pause()
{
new WaitForEndOfFrame();
players = GameObject.FindGameObjectsWithTag("PlayerOb");
foreach (GameObject player in players)
player.GetComponent<InputHandler>().SetPlayerActionsEnabled(false);
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
AudioListener.pause = true;
Paused = true;
}
public void OnPause(InputAction.CallbackContext context)
{
if (Paused)
Resume();
else
Pause();
}
public void Settings()
{
Debug.Log("Make me");
}
public void Quit()
{
Resume();
SceneManager.LoadScene("Title");
}
private void OnEnable()
{
pause = pInputs.Menu.Pause;
pause.Enable();
pause.performed += OnPause;
}
private void OnDisable()
{
pause.Disable();
}
}
My pause script is the above, fairly straightforward, and works perfectly when playing my game single-player. Once a second player is added while the game isn’t paused (when paused, they’re stuck as they should, will disable or delay adding players while paused later) the players can freely move around and the game is not paused.
The SetPlayerActions is as follows:
public void SetPlayerActionsEnabled(bool value) //Sorts out input handling when pausing
{
var actions = playerInput.actions.FindActionMap("Player");
foreach (var action in actions)
{
if (value)
action.Enable();
else
action.Disable();
}
actions = playerInput.actions.FindActionMap("Menu");
foreach (var action in actions)
{
if (value)
action.Disable();
else
action.Enable();
}
}
This shouldn’t be an issue but I’m sure someone would ask. The canvas and player are in a prefab together. How do I fix this so that the game will pause when in multiplayer. The eventsystem is a multiplayer eventsystem.