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 UniluckStudios · Jul 21, 2017 at 04:10 AM · spawnbuttonspausepause gamepausing

How to Make Pressing a Button Not Trigger Other Events?

In my game I have a ball spawner that spawns a ball every time I tap the screen. I also have a pause button. When I tap the paused button, the spawner still spawns the ball even though I have told it not to using code. Here is my code:

Spawner: public GameObject Ball; // Use this for initialization void Start () {

 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetMouseButtonDown (0)) {
         Instantiate (Ball, transform.position, Quaternion.identity);
     }
 }

}

Pause Buttons: public class PauseControllerScript : MonoBehaviour {

 public string mainMenu;

 public bool isPaused;

 public GameObject pauseMenuCanvas;

 public GameObject spawner;

 public GameObject cups;

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     if (isPaused) {
         pauseMenuCanvas.SetActive (true);
         Time.timeScale = 0f;
         spawner.GetComponent <BallSpawn> ().enabled = false;
     } else {
         pauseMenuCanvas.SetActive  (false);
         Time.timeScale = 1f;

     }
         

 }

 public void Resume () {
 
     isPaused = false;

 }

 public void Quit () {
     SceneManager.LoadScene ("StartScreen");
 
 }

 public void Paused () {
     isPaused = !isPaused;
 }

 public void Yes () {
     isPaused = true;
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by vir1234 · Jul 21, 2017 at 04:13 AM

 void Update ()
  {
      if (Input.GetMouseButtonDown (0)  && !isPaused) 
 {
          Instantiate (Ball, transform.position, Quaternion.identity);
      }
  }


and why r u using your pause code in Update() use On button click and if you want to avoid the click on pause bt dont use getmousebuttondown use ui image filled with screen size

Comment
Add comment · Show 3 · 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 SohailBukhari · Jul 21, 2017 at 06:37 AM 0
Share

you always instantiate balls in the spawner class and you didn't check anywhere in the spawner class whether game is paused or not, so before instantiating make sure your game is not paused as @vir1234 suggested. Other thing is that you have written code in the Update method which is not good technique. in your PauseControllerScript make a UnityEvent and invoke that event when you you paused your game ins$$anonymous$$d of checking in Update.

 public UnityEvent GamePausedEvent;
  void Start () {
        GamePausedEvent.AddListener(delegate {
        if (isPaused) {
          pause$$anonymous$$enuCanvas.SetActive (true);
          Time.timeScale = 0f;
          spawner.GetComponent <BallSpawn> ().enabled = false;
      } else {
          pause$$anonymous$$enuCanvas.SetActive  (false);
          Time.timeScale = 1f;
      }
 
        });
  }

invoke this event where are are pausing your game. GamePausedEvent.Invoke();

avatar image UniluckStudios · Jul 21, 2017 at 06:44 AM 0
Share

I am an inexperienced programmer :-). I was watching a tutorial series and it told me to add the pause code in the update

avatar image SohailBukhari UniluckStudios · Jul 21, 2017 at 08:40 AM 0
Share

Good learn other ways also to do same thing with other good techniques.I am not asking for adding code in the update method but code is executing in every frames and we don't want that extra work to processor so thats why i suggested. Writing code in update is not an issue just its for performances. I hope you will be experienced programmer in future :)

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

69 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

Related Questions

Is a global static boolean a good implementation for pausing my game? 2 Answers

Pause Menu Messes Up Game 1 Answer

Make button be above another button? 1 Answer

Pause game when user pulls down the notification panel 0 Answers

When I pause the game the clone does not stop 0 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