Pause not working when multiple players are on screen

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.

This has no effect. It’s not in a coroutine and it’s not being yielded.

Does that mean that every player instance also has a PauseMenu component? If so, that component exists twice and will run its code twice. Which is probably why a second player causes it to fail.

I suspect that one player pauses, which runs OnPause, this sets the static Paused to true. Then the second component runs OnPause, finds that Paused is set, and resumes. Be vary of static fields!

Only player-specific UI should be in the player prefab, such as the player’s healthbar.

But any UI that is for both players like anything fullscreen or global, like pause, should not be parented to the players.

That wait is a leftover I must’ve forgotten to delete so thanks for pointing that out.

Yes each player has a pause, the idea was that later I’d add a line checking whose pause it was so only that player could unpause.

However you’re probably right, I couldn’t figure out how to make a global ui work given the input actions won’t match since the prefabs all have “PlayerInputs (Clone)” which unity considers different from “PlayerInputs” and attaching the UI Inputs in code wasn’t working as intended but I did that late at night so I should try again while more awake.