- Home /
how to make my gun not fire while paused
i tried to make my gun not fire while the game is paused becasue the gun fires by using update to detect a mouse click but its not working here is my code:
using System.Collections;
using UnityEngine;
public class gunfire : MonoBehaviour {
public Transform chargeSpawn;
public GameObject bubble;
public GameObject growingBubble;
public float growRate = 0.025f;
public float damage;
public int damageModifier = 2;
PauseMenu pausemenu;
public Transform spawnpoint;
public bool isGrowing = false;
IEnumerator growCoroutine;
void Start()
{
growCoroutine = growCor();
}
public void firePause(PauseMenu pausemenu) {
this.pausemenu = pausemenu;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (pausemenu.gameIsPaused == false)
{
}
else {
growingBubble = Instantiate(bubble, spawnpoint.position, spawnpoint.rotation);
growStart();
}
}
if (Input.GetMouseButtonUp(0))
{
growStop();
bubble.transform.localScale = new Vector3(1f, 1f, 1f);
}
}
IEnumerator growCor()
{
while (isGrowing)
{
growingBubble.transform.localScale += Vector3.one * growRate;
growingBubble.transform.position = spawnpoint.position;
var radius = growingBubble.transform.localScale.y;
if (radius >= 4)
{
growStop();
}
yield return null;
}
}
void growStart() {
if (!isGrowing) {
isGrowing = true;
StartCoroutine(growCoroutine);
}
}
void growStop() {
if (isGrowing)
{
StopCoroutine(growCoroutine);
isGrowing = false;
Vector3 explosionPos = growingBubble.transform.position;
var radius = growingBubble.transform.localScale.y;
float speed = ((400 / radius));
damage = (damageModifier*radius);
Rigidbody projectile = growingBubble.GetComponent<Rigidbody>();
projectile.velocity = transform.TransformDirection(new Vector3(speed, 0, 0));
projectile.AddExplosionForce(5f, explosionPos, 5f, 0f);
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Projectile")
{
Physics.IgnoreCollision(growingBubble.GetComponent<Collider>(), growingBubble.GetComponent<Collider>());
}
}
}
does anyone see what is wrong or have a better idea
Answer by LijuDeveloper · Nov 19, 2019 at 06:49 AM
Try following changes
Add a class
public class Global
{
public static bool isGamePaused;
}
And make it Global.isGamePaused = true;
whenever the game is paused.
private void Update()
{
if (Global.isGamePaused)
{
return;// while pausing following code will skip
}
if (Input.GetMouseButtonDown(0))
{
if (Global.isGamePaused != false)// double check
{
growingBubble = Instantiate(bubble, spawnpoint.position, spawnpoint.rotation);
growStart();
}
}
if (Input.GetMouseButtonUp(0))
{
growStop();
bubble.transform.localScale = new Vector3(1f, 1f, 1f);
}
}
IEnumerator growCor() {
while (isGrowing && !Global.isGamePaused) //add this condition to all your coroutines
{
growingBubble.transform.localScale += Vector3.one * growRate;
growingBubble.transform.position = spawnpoint.position;
var radius = growingBubble.transform.localScale.y;
if (radius >= 4)
{
growStop();
}
yield return null;
}
}
it doesn't fire while the game is running either and i get an object reference is not set to an instant error now
Please check new changes and try to debug using Debug.log methos
Answer by Cornelis-de-Jager · Nov 19, 2019 at 10:27 PM
I would say desentralize your code. Create a GameManager.
public class GameManager : MonoBehaviour {
public static GameManager Instance { get; private set; }
void Awake() {
if (Instance == null) {Instance = this; } else if (Instance != this) {Destroy(this); }
DontDestroyOnLoad(gameObject);
}
// <FUTURE CODE GOES HERE> //
}
This way you have an easy way to access your global data. Next is to create an Enum that controls game states.
public enum GameState {
Default,
MainMenu,
PauseMenu,
Running
}
Add that to your Game Manager
public GameState state;
Now in your Gun script simply do this:
void Update () {
if (GameManager.Instance.state == GameState.Running)
HandleShooting(); // Move your cude currently in update to HandleShooting function
}
i get the error code at the
if (Game$$anonymous$$anager.Instance.state == GameState.Running)
You are trying to get instance before it created that's why error,
modify like this
public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour {
private static Game$$anonymous$$anager instance;
public static Game$$anonymous$$anager Instance { get
{
if (instance == null)
{
instance = FindObjectOfType<Game$$anonymous$$anager >();
}
return instance;
}
; private set; }
void Awake() {
if (Instance == null) {Instance = this; } else if (Instance != this) {Destroy(this); }
DontDestroyOnLoad(gameObject);
}
// <FUTURE CODE GOES HERE> //
}
the private set says it needs a body. what does that mean sorry im new to c#
Your answer
![](https://koobas.hobune.stream/wayback/20220612215000im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Detection bullets hit 2 Answers
Need help with my gun script! 1 Answer
Help with gun accuracy in degrees. 3 Answers
Bullets Based on Orientation 1 Answer
How to make a gun shoot? 2 Answers