Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Having trouble resuming my game from the menu

Discussion in 'Editor & General Support' started by WillClarkguru, Feb 21, 2024.

  1. WillClarkguru

    WillClarkguru

    Joined:
    Nov 7, 2018
    Posts:
    2
    I am having trouble with resuming my game from the ingame menu. I am using a bool to indicate if I am paused or not GameIsPaused and when I set it to false in my resume function it wouldnt resume and the menu would still be there. I had added a check for this bool in the gun shooting script and it seems like that is getting in the way of my resume function. What am I doing wrong here please help!

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7. using StarterAssets;
    8.  
    9. public class PauseMenu : MonoBehaviour
    10. {
    11.     public bool GameIsPaused = false;
    12.     public GameObject pauseMenuUI;
    13.     public GameObject soundSettingsUI;
    14.  
    15.  
    16.     private CharacterController _controller;
    17.     private StarterAssetsInputs _input;
    18.  
    19.     private void Start()
    20.     {
    21.         pauseMenuUI.SetActive(false);
    22.         Time.timeScale = 1;
    23.         GameIsPaused = false;
    24.         _input = transform.root.GetComponent<StarterAssetsInputs>();
    25.  
    26.     }
    27.  
    28.     private void Update()
    29.     {
    30.         if (_input.PauseGame)
    31.         {
    32.             if (!GameIsPaused)
    33.             {
    34.                 Pause();
    35.             }
    36.             else
    37.             {
    38.                 return;
    39.             }
    40.         }
    41.     }
    42.  
    43.     public void Restart()
    44.     {
    45.         AkSoundEngine.StopAll();
    46.         Time.timeScale = 1;
    47.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    48.     }
    49.  
    50.     public void soundSettings()
    51.     {
    52.         soundSettingsUI.SetActive(true);
    53.     }
    54.     public void soundSettingsClose()
    55.     {
    56.         soundSettingsUI.SetActive(false);
    57.     }
    58.  
    59.     public void Resume()
    60.     {
    61.         Cursor.lockState = CursorLockMode.Locked;
    62.         Cursor.visible = false;
    63.         pauseMenuUI.SetActive(false);
    64.         Time.timeScale = 1;
    65.         GameIsPaused = false;
    66.     }
    67.  
    68.  
    69.     public void Pause()
    70.     {
    71.         Cursor.lockState = CursorLockMode.None;
    72.         Cursor.visible = true;
    73.         pauseMenuUI.SetActive(true);
    74.         Time.timeScale = 0;
    75.         GameIsPaused = true;
    76.     }
    77.  
    78.  
    79.     public void Quit()
    80.     {
    81.         Application.Quit();
    82.     }
    83. }
    84.  
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using StarterAssets;
    6.  
    7. public class Gun_01 : MonoBehaviour
    8. {
    9.     private StarterAssetsInputs _input;
    10.     [SerializeField] private GameObject _bullet_01;
    11.     [SerializeField] private GameObject bulletPointPrim;
    12.     [SerializeField] private GameObject bulletPointSec;
    13.     [SerializeField] private float bulletSpeedPrim = 600f;
    14.     public int _primaryAmmoCount;
    15.     public int _secondaryAmmoCount;
    16.     private Player_Audio playerAudio;
    17.     private Animator _animator;
    18.     private PauseMenu menuSystem;
    19.  
    20.  
    21.  
    22.     void Start()
    23.     {
    24.         menuSystem = GetComponentInParent<PauseMenu>();
    25.         _animator = GetComponentInParent<Animator>();
    26.         _input = transform.root.GetComponent<StarterAssetsInputs>();
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.  
    33.         if (menuSystem.GameIsPaused == false)
    34.         {
    35.             if (_input.ShootPrimary)
    36.             {
    37.                 ShootPrimary();
    38.                 _input.ShootPrimary = false;
    39.             }
    40.  
    41.             if (_input.ShootSecondary)
    42.             {
    43.                 ShootSecondary();
    44.                 _input.ShootSecondary = false;
    45.             }
    46.         }
    47.  
    48.  
    49.     }
    50.  
    51.     void ShootPrimary()
    52.     {
    53.         _animator.SetTrigger("shootPrimary");
    54.         GameObject bullet = Instantiate(_bullet_01, bulletPointPrim.transform.position, transform.rotation);
    55.         bullet.GetComponent<Rigidbody>().AddForce(transform.forward * bulletSpeedPrim);
    56.         Destroy(bullet, 1);
    57.  
    58.     }
    59.  
    60.     void ShootSecondary()
    61.     {
    62.         Debug.Log("ShootSecondary!");
    63.     }
    64. }
    65.  
     
  2. WillClarkguru

    WillClarkguru

    Joined:
    Nov 7, 2018
    Posts:
    2
    Solved. It was a problem with the input manager