[Help] Pause Game And Lock Input Keys

Hello everyone, first of all I wanna apologise for making such thread but i’m new into Unity and new also to any kind of programing languages either Javascript or Csharp. I’ve developed a game called “Chopper Run” available at PlayStore already and I was looking for some new updates that came in mind and one of it was the Pause Button. The game is similar to “Flappy bird” a 2d Sidescroller some objects coming up in the way and we only have to jump to avoid them. For now the game is simply this…jump to avoid objects, I have a C# script to players movement that is this one:

    using UnityEngine;
     
    public class Player : MonoBehaviour
    {
        // The force which is added when the player jumps
        // This can be changed in the Inspector window
        public Vector2 jumpForce = new Vector2(0, 300);
       
        // Update is called once per frame
        void Update ()
        {
            // Jump
            if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) )
            {
                rigidbody2D.velocity = Vector2.zero;
                rigidbody2D.AddForce(jumpForce);
            }
           
            // Die by being off screen
            Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
            if (screenPosition.y > Screen.height || screenPosition.y < 0)
            {
                Die();
            }
        }
       
        // Die by collision
        void OnCollisionEnter2D(Collision2D other)
        {
            Die();
        }
       
        void Die()
        {
            Application.LoadLevel(Application.loadedLevel);
        }
    }

everytime we click “Space” or “Left Mouse Button” or “Touch Screen” it “flaps” it jumps… simple.

Now I made a UI Button and attached a JavaScript to it with the pause code that is on a GameObject with all ObjectCreator scripts that looks like this:

    #pragma strict
     
    var paused : boolean = false;
    private var savedTimeScale:float;
     
         function OnGUI() {
     
                   if(paused)
                        {
                        savedTimeScale = Time.timeScale;
                        Time.timeScale = 1.0;
                        paused=false;
                         }
                   else
                        {
                        Time.timeScale = savedTimeScale;
                        Time.timeScale = 0.0;
                        paused = true;
                        }
           
        }

And also I have a “Click to Fly” Sprite with a script attached, similar to “Flappy Bird” Button called “tap to Flap” at the start of the game with this code:

    using UnityEngine;
    using System.Collections;
     
    public class Click_To_Fly : MonoBehaviour {
     
        static bool sawOnce = false;
     
        // Use this for initialization
        void Start () {
            if(!sawOnce) {
                GetComponent<SpriteRenderer>().enabled = true;
                Time.timeScale = 0;
            }
     
            sawOnce = false;
        }
       
        // Update is called once per frame
        void Update () {
            if(Time.timeScale==0 && (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) ) {
                Time.timeScale = 1;
                GetComponent<SpriteRenderer>().enabled = false;
     
            }
        }
    }

Problems:

1- With “Click To Fly” [ON] and “Pause Script” [OFF] it starts stoped as it should, you click and “Click to Fly” sprite disapears and when you click “Pause” UI Button once it jumps, Twice it jumps and then Pauses the game and if you pause the game and click on screen or with Space/LeftMouse it resumes the game.

2- With “Click To Fly” [ON] and “Pause Script” [ON] it starts already flying, the “Click To Fly” sprite don’t go off the Screen and you click once to pause the game and if you click/space/Touch on screen it saves those inputs and if you click Pause Button it loads all those Jumps and die.

3- With “Click To Fly” [OFF] and “Pause Script” [ON] it starts already flying with no “Click To Fly” button, when you click pause it pauses the game but if you click/space/Touch in game screen it saves those “Jumps” and after you click “Pause Button” it will load all those clicks like in “Problem 2-”.

Help:

1- If possible I wanted a “Click To Fly” Button or Sprite with Script Attached like it is now + a Pause/Resume Ui Button like it is now but that don’t allows Key/Touch inputs, so when we hit Pause menu it doesn’t allow/save those key/touch inputs anymore and at “resume game” (click on pause button again) it doesnt Jump as many as the “paused” clicks/touches.

Thanks in advance and sorry for my bad spelling.

you just made me find out i had this problem in my own game :smiley:
and the way i solved it was to make a bool called isPaused in my GUIManager Script and then all the places where i want the things not to work while the game is paused i put and if statement around them that checks if my GUIManager variable GameIsPasued == false if its = to false do things if its true do nothing

example code

public GUIManager guiManager;

void Start ()
{
    guiManager = FindObjectOfType<GUIManager>();
}

void Update ()
{
   if (guiManager.GetComponet<GUIManager>().GameIsPaused == false)
   {
        // do things normally
   }
   else
   {
        // do nothing
   }
}

this code might have some typos but it should give you an idea of what i mean hope it helps :slight_smile:

Another way is to wrap the input system calls with your own so you instead do something like this:

using UnityEngine;

public class InputExt {
	public static float GetAxis(string axis) {
		if(!GameMgr.instance.paused) {
			return Input.GetAxis(axis);
		}

		return 0;
	}
}

and then call InputExt.GetAxis(…) everywhere instead.