- Home /
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); } } }
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
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
Follow this Question
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