Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
4 captures
13 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
1
Question by Brijac · Nov 19, 2020 at 09:08 PM · pause

Disable game controller while pause is active

Hello, I'm making a Flappy Bird game and I added the pause button the to the game. When I press the pause button the game is paused by Time.timeScale = 0; but if I press the jump button (in my case is the Space button) and then hit the resume button the bird jumps instantly when the game resumes. That means that the controller is still working while the pause is activated. How can I disable the controller?

This is the pause script:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class Pause : MonoBehaviour { Image img;

 public Sprite playSprite;
 public Sprite pauseSprite;

 private void Start()
 {
     img = GetComponent<Image>();
 }
 public void OnPausedGame()
 {
     if(GameManager.gameIsPaused == false)
     {
         Time.timeScale = 0;
         img.sprite = playSprite;
         GameManager.gameIsPaused = true;
     }
     else
     {
         Time.timeScale = 1;
         img.sprite = pauseSprite;
         GameManager.gameIsPaused = false;
     }
 }

}

This is my controller script:

private void Update() { if (isDead == false) { if (Input.GetKey(KeyCode.Space)) { rb2d.AddRelativeForce(Vector3.up); } } }

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
Best Answer

Answer by GGfazo · Nov 19, 2020 at 09:27 PM

Try getting the player object and disabling the script of movement when game is paused, and enable it when not. If you don't know how to disable a script use

 GameObject.Find("objectname").GetComponent(<Component>).enabled =  false;

That is for disable, for enable just replace "false" with "true". And you must replace "objectname" with the name of the player object and change (component) with the movement script's name. So it will be something like this:

   if(GameManager.gameIsPaused == false)
  {
      Time.timeScale = 0;
      img.sprite = playSprite;
      GameManager.gameIsPaused = true;
      gameObject.Find("Player").GetComponent(<MovementScript>).enabled =  true;
  }
  else
  {
      Time.timeScale = 1;
      img.sprite = pauseSprite;
      GameManager.gameIsPaused = false;
      gameObject.Find("Player").GetComponent(<MovementScript>).enabled =  false;
  }

Don't forget to replace what I said before. If you want you can replace gameObject.Find("player") to gameObject.FindWithTag("player) JUST IF YOU WANT @Brijac

Comment
Add comment · Show 1 · 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 Brijac · Nov 19, 2020 at 10:21 PM 1
Share

I had to change the script you wrote a bit, but your answer is correct.

Instead of this:

gameObject.Find("Player").GetComponent().enabled = true;

I used this:

GameObject.Find("Player").GetComponent().enabled = true;

Thanks man, I really appreciate it!

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

139 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 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

getting gui error you can only call functions from inside OnGui. 1 Answer

Pause and resume coroutine 0 Answers

Pause Game 1 Answer

Pausing the game after player object gets destroyed 1 Answer

Android service plugin with OnApplicationPause issue 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