- Home /
Pause game automatically on scene start and then resume on key press
So, I don't want my game to start immediately when the scene loads, I want my game to be paused from the start and then resumed on Space key pressed.
So, scene loads, game is paused, panel (on which there is text like "press Space button to start") is active and then when the player presses Space the game should resume, panel (on which there is text like "press Space button to start") should become inactive and audio of the game should start playing. How can I do this?
This is what I did:
Created this script and put it on that panel that is active in the beginning:
public class SceneStartPause : MonoBehaviour
{
public GameObject SceneStart;
private void Start()
{
Time.timeScale = 0;
SceneStart.SetActive(true);
}
public void OnSceneStart()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Time.timeScale = 1;
SceneStart.SetActive(false);
}
}
}
The panel is active and game is paused on scene start but nothing happens when I press the Space button, why is that? Also how could I add audio to start playing when the Space button is pressed?
Answer by qsp18 · Dec 07, 2020 at 02:50 PM
Put in the start method: void Start() { time.timescale = 0; }
and then in update function void Update() { if (Input.anyKey) { time.timescale = 1; } }
It worked, I made stupid mistake by not simply using Update..
Do you maybe know how can I also make audio play when the key is pressed?
Add an AudioSource-Component to your Object. Check the "Loop" Box, if you want audio to be constantly played. When you put your timescale to 1, just GetComponent().Play()
To have better Performance, you can even start a coroutine to check, if any key is pressed and then yield break the IEnumerator if it is pressed.
Your answer
![](https://koobas.hobune.stream/wayback/20220613021223im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Optimise Menu 3 Answers
UNET: how to send network data from OnApplicationPause? 1 Answer
When I unpause my game I need to click to hide my cursor again when I pause and unpause my game. 2 Answers
Problem with Unity’s FPSController camera 2 Answers
Adding to delegate with multiple instances of the same object 1 Answer