- Home /
The question is answered, right answer was accepted
Is there any other method to pause a game apart from using Time.timescale=0; ?,Can I pause my game other than setting Time.timeScale = 0;
Hi everyone, I am a 17 yr old novice programmer and have a hard time creating a fully functional pause menu. My issue is that the only thing my code can do is to freeze the time but not the touch input for the player into the game itself. A clearer example is that I can pause the game and nothing moves at all but if I press anywhere else in the game screen apart from the buttons severally and then I resume the game, the player receives all of those inputs (the inputs after tapping many times when it was paused) at once causing it to move uncontrollably and crashes in walls unexpectedly and dies. I don't know if it is possible to disable touch screen functionality (except for the navigation buttons) using code. By the way, the game play moves the player not with buttons but with touch just like Subway Surfers or Angry Birds. Here is the code. I'll appreciate any help I can get. Thanks for your time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Pause : MonoBehaviour
{
public GameObject PauseButton, PausePanel; //References the pause button and pause menu
public void OnPause()
{
Time.timeScale = 0;//Freezes time but not game player input
PauseButton.SetActive(false);//Disables the pause button when game is paused
PausePanel.SetActive(true);//Enables the pause menu when game is paused
}
public void OnUnPause()
{
Time.timeScale = 1;//Unfreezes time when game is resumed
PauseButton.SetActive(true); //Re-enables pause button during gameplay after resuming
PausePanel.SetActive(false);//Disables pause menu
}
}
,Hello everyone. I am a 17-year old novice programmer and having a hard time to pause my game. The issue is that after pressing the pause button on the game screen, the game stops only the time but not user input inside the game. A clearer example is that when I pause the game, I can tap anywhere else on the screen other than the resume button, when I finally press the resume or main menu button to resume, the game receives the multiple touches I had input while it was paused and translates these touches to cause the player to move everywhere as if I have pressed the screen a hundred times at once during gameplay. Is there a way to disable touch input for the game completely (but still enable touch input for the "Resume" and "Back to Main Menu" buttons) when the game is paused so that it won't execute all of those inputs at that moment when I resume the game? I'll appreciate any help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Pause : MonoBehaviour
{
public GameObject PauseButton, PausePanel; //To reference the Pause button and Pause panel
public void OnPause()
{
Time.timeScale = 0;//Freezes time only during pause but game's player still receives input
PauseButton.SetActive(false); //Disables the pause button on the screen when game is paused
PausePanel.SetActive(true); //Enables the panel which contains the "Resume" and "Main menu"
}
public void OnUnPause()
{
Time.timeScale = 1; //Unfreezes the game when "Resume" is pressed
PauseButton.SetActive(true); //Re-displays the pause button when game resumes
PausePanel.SetActive(false); //Disables the pause menu
}
}
Answer by Slimer37 · Jul 08, 2020 at 06:17 AM
You could try an event system or have your control scripts disable themselves when the game is paused. Maybe you could disable those scripts in that script. They still receive inputs, but since time is frozen, everything it tried happens at once.
Hey there @Slimer37. That's an awesome idea but I don't know the scripting to do such a task. Can you please help me out with that script. :) I started using Unity last month and don't know much. :(
public Player$$anonymous$$ovementScript _player$$anonymous$$ovement;
public void Pause()
{
Time.timescale = 0;
_player$$anonymous$$ovement.enabled = false;
}
public void Resume()
{
Time.timescale = 1;
_player$$anonymous$$ovement.enabled = true;
}
Answer by kot2202 · Jul 08, 2020 at 04:38 PM
Disable graphic raycaster component in canvases that control movement