Search Unity

Resolved Trying to create a Pause menu with new Input system, Unity Editor crashes

Discussion in 'Input System' started by felipe_slv, Apr 1, 2021.

  1. felipe_slv

    felipe_slv

    Joined:
    Jan 3, 2020
    Posts:
    4
    Hi there. I have a released single player game (computer) and I decided to update it in order to learn the new Input System and also providing players with controller support (first release is mouse and keyboard only).

    I'm using Unity 2019.4.16f1.

    The following is the code I created for my pause system, but I have some problems with it:
    When I have the playerInput.SwitchCurrentActionMap lines, it breaks Unity Editor.

    When I just leave the Debug.log to check the input behavior, console shows me the action is triggered multiple times (one button press gives me 3 pairs of "Game Paused" / "Game Unpaused").

    For this specific behaviour the bindings in my Input Actions Asset are set to Button, but they give me the same result if I set them to value.

    Here's the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. using UnityEngine.UI;
    6.  
    7.  
    8. public class PauseUnpause : MonoBehaviour
    9. {
    10.     public bool isPaused;
    11.     public PlayerInput playerInput;
    12.     [SerializeField] private GameObject pauseMenuPanel;
    13.    
    14.          
    15.     public void TogglePause(InputAction.CallbackContext context)
    16.     {
    17.         if (!pauseMenuPanel.activeInHierarchy)
    18.         {
    19.             Time.timeScale = 0;
    20.             playerInput.SwitchCurrentActionMap("UI");
    21.             pauseMenuPanel.SetActive(true);
    22.             InputSystem.settings.updateMode = InputSettings.UpdateMode.ProcessEventsInDynamicUpdate;
    23.             Debug.Log("Game Paused");
    24.             //isPaused = true;
    25.         }
    26.        
    27.         if (pauseMenuPanel.activeInHierarchy)
    28.         {
    29.             Time.timeScale = 1;
    30.             playerInput.SwitchCurrentActionMap("Player");
    31.             pauseMenuPanel.SetActive(false);
    32.             InputSystem.settings.updateMode = InputSettings.UpdateMode.ProcessEventsInFixedUpdate;
    33.             Debug.Log("Game Unpaused");
    34.            
    35.         }
    36.  
    37.     }
    38.  
    39.    
    40. }
     
  2. felipe_slv

    felipe_slv

    Joined:
    Jan 3, 2020
    Posts:
    4
    I managed to solve the problem for toggling the pause menu on and off. All I needed to do was changing the second "if" to an "else". Debug.Log still gives me the double messages but the behaviour is correct. I tried changing the tag to "solved" but I couldn't find the option (first time I posted a question here
     
  3. yosefstudios

    yosefstudios

    Joined:
    May 8, 2015
    Posts:
    129
    To update the title flair, go to Thread Tools and select Edit Title. Once you're there, select the flair at the beginning of your title and change it to whatever you desire.
     
    felipe_slv likes this.
  4. felipe_slv

    felipe_slv

    Joined:
    Jan 3, 2020
    Posts:
    4
    Thanks, done!
     
    yosefstudios likes this.
  5. rjmatthews62_unity

    rjmatthews62_unity

    Joined:
    Jul 6, 2021
    Posts:
    1
    That code works pretty well, with the caveat that you need to make sure the Player Input component HAS a UI map... the default Third Person Starter Asset does NOT.
    Also, adding:
    Code (CSharp):
    1.             Cursor.lockState = CursorLockMode.None;
    can be useful.