Search Unity

Need help with pausing a script when opening pause menu

Discussion in 'Scripting' started by Kal3b, Apr 18, 2019.

  1. Kal3b

    Kal3b

    Joined:
    Apr 15, 2019
    Posts:
    14
    So I have created a Pause Menu I my FPS game. I have managed to get the Main Camera to pause however whenever you click you will still fire a shout from the pistol. I need a solution where when "ESC" is pressed the pistol script disappears and when the resume button is pressed it reappears. I also have a problem where on the first time of pausing the menu just appears as a still image but if the game is resumed then paused again it works perfectly fine. Thanks guys I will include both PauseMenu and Pistol.

    Pistol:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Pistol : MonoBehaviour
    6. {
    7.     // Damage of the gun is set to 10. Range is set to 100.
    8.     public float damage = 10f;
    9.     public float range = 100f;
    10.  
    11.     public Camera fpsCam;
    12.     public ParticleSystem disperseFlash;
    13.     public Transform parent;
    14.     public GameObject prefab;
    15.     public Vector3 position1;
    16.     public Vector3 position2;
    17.  
    18.     public int maxAmmo = 10;
    19.     private int currentAmmo;
    20.     public float reloadTime = 1f;
    21.     private bool isReloading = false;
    22.  
    23.     public Animator animator;
    24.  
    25.     public int ammo;
    26.     public bool isFiring;
    27.     public Text ammoDisplay;
    28.     public bool reloadText;
    29.  
    30.     void Start ()
    31.     {
    32.         if (currentAmmo == -1)
    33.         currentAmmo = maxAmmo;
    34.     }
    35.  
    36.     // Update is called once per frame
    37.     void Update()
    38.     {
    39.         if (isReloading)
    40.             return;
    41.        
    42.         // When "R" is pressed the reloading animation is played and currentAmmo is set to maxAmmo(10).
    43.         ammoDisplay.text = ammo.ToString();
    44.         if (Input.GetKeyDown(KeyCode.R) && !reloadText && ammo <= 9)
    45.         {
    46.             reloadText = true;
    47.             ammo = +10;
    48.             reloadText = false;
    49.             StartCoroutine(Reload());
    50.             return;
    51.         }
    52.      
    53.             //This fires a Raycast when Fire1 or Mouse1 has been pressed.
    54.             if (Input.GetButtonDown("Fire1"))
    55.         {
    56.             Shoot();
    57.         }
    58.        
    59.         //When "Fire1" is pressed the currentAmmo count is declined by -1.
    60.         ammoDisplay.text = ammo.ToString();
    61.         if (Input.GetButtonDown("Fire1") && !isFiring && ammo > 0)
    62.         {
    63.             isFiring = true;
    64.             ammo--;
    65.             isFiring = false;
    66.  
    67.         }
    68.  
    69.  
    70.  
    71.     //Reload animation delay and raycast firing delay.
    72.     IEnumerator Reload()
    73.     {
    74.         isReloading = true;
    75.         Debug.Log("Reloading...");
    76.  
    77.         animator.SetBool("Reloading", true);
    78.  
    79.         yield return new WaitForSeconds(reloadTime);
    80.  
    81.         animator.SetBool("Reloading", false);
    82.  
    83.         currentAmmo = maxAmmo;
    84.         isReloading = false;
    85.     }
    86.  
    87.  
    88.  
    89.  
    90.     //Raycast casts a ray within the range set then provides an output in debug log if anything has been hit.
    91.     void Shoot ()
    92.     {
    93.         //Plays Particle System
    94.         disperseFlash.Play();
    95.  
    96.         currentAmmo--;
    97.  
    98.         RaycastHit hit;
    99.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    100.         {
    101.             Debug.Log(hit.transform.name);
    102.  
    103.             TargetScript target = hit.transform.GetComponent<TargetScript>();
    104.             if (target != null)
    105.             {
    106.                 target.TakeDamage(damage);
    107.  
    108.                 //When Chungus Fruit is Destroyed 2 more are spawned.
    109.                 Vector3 position1 = new Vector3(Random.Range(-47f, 47f), 10f, Random.Range(-47f, 47f));
    110.                 Instantiate(prefab, position1, Quaternion.identity, parent);
    111.                 Vector3 position2 = new Vector3(Random.Range(-47f, 47f), 10f, Random.Range(-47f, 47f));
    112.                 Instantiate(prefab, position2, Quaternion.identity, parent);
    113.             }
    114.         }
    115.     }
    116. }
    117.  
    118.  
    PauseMenu
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PauseMenu : MonoBehaviour
    6. {
    7.     public static bool GameIsPaused = false;
    8.  
    9.     public GameObject pauseMenuUI;
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         if (Input.GetKeyDown(KeyCode.Escape))
    15.         {
    16.                 if (GameIsPaused)
    17.                 {
    18.                     Resume();
    19.                 } else
    20.                 {
    21.                     Pause();
    22.                 }
    23.            
    24.         }
    25.     }
    26.    
    27.     public void Resume ()
    28.     {
    29.         pauseMenuUI.SetActive(false);
    30.         Time.timeScale = 1f;
    31.         GameIsPaused = false;
    32.  
    33.     }
    34.  
    35.  
    36.     void Pause ()
    37.     {
    38.         pauseMenuUI.SetActive(true);
    39.         Time.timeScale = 0.0f;
    40.         GameIsPaused = true;
    41.     }
    42.  
    43.     public void QuitGame()
    44.     {
    45.         Debug.Log("Quit");
    46.         Application.Quit();
    47.     }
    48.  
    49. }
    50.  
    51.  
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Every script has .enabled property. For your gun script, set it to false when pause menu appears.
     
  3. Kal3b

    Kal3b

    Joined:
    Apr 15, 2019
    Posts:
    14
    How would I do this? (Sorry I am new to unity) could you either link me somewhere or write me some script. thanks