Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_i0W6HQ-YEHqs4Q · Nov 19, 2019 at 12:18 AM · bulletgunpause

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
  }

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_i0W6HQ-YEHqs4Q · Nov 19, 2019 at 09:20 PM 0
Share

it doesn't fire while the game is running either and i get an object reference is not set to an instant error now

avatar image LijuDeveloper unity_i0W6HQ-YEHqs4Q · Nov 20, 2019 at 12:25 PM 0
Share

Please check new changes and try to debug using Debug.log methos

avatar image
0

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            
     
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_i0W6HQ-YEHqs4Q · Nov 19, 2019 at 10:42 PM 0
Share

i get the error code at the

if (Game$$anonymous$$anager.Instance.state == GameState.Running)

avatar image Cornelis-de-Jager unity_i0W6HQ-YEHqs4Q · Nov 20, 2019 at 02:58 AM 1
Share

Well what is the error?

avatar image LijuDeveloper unity_i0W6HQ-YEHqs4Q · Nov 20, 2019 at 12:30 PM 0
Share

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> //
  }




avatar image unity_i0W6HQ-YEHqs4Q LijuDeveloper · Nov 20, 2019 at 08:48 PM 0
Share

the private set says it needs a body. what does that mean sorry im new to c#

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

117 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges