Code (csharp): // to display a message in the Text Status GUIText object var DisplayGameStatus : GUIText; // determines if the game is paused by using a simple ON or OFF state set initially to OFF/0 var AreWePaused : int = 0; // functions that are run when the game play levels are first loaded and run function Start () { // when the level starts we hide the cursor and lock the cursor position to middle of screen // remember to show and unlock cursor when level ends and score needs to be submitted Time.timeScale = 1; Screen.lockCursor = true; Screen.showCursor = false; } function Update () { if (Input.GetButtonUp ("Pause") AreWePaused == 0) { Time.timeScale = 0; AreWePaused = 1; DisplayGameStatus.guiText.text = "Paused"; } if (Input.GetButtonUp ("Pause") AreWePaused == 1) { Time.timeScale = 1; AreWePaused = 0; DisplayGameStatus.guiText.text = ""; } } This isn't working. I have defined an extra input called Pause and associated it with the letter "p". But when you hit "p" in game, nothing. This is attached to an empty game object in game Thoughts?
When you "pause", you first change AreWePaused to 1, which works. Then you turn right around in the same frame where Input.GetButtonUp("Pause") is still true and check if AreWePaused is = 1, which is now is since it was just set to 1. How about Code (csharp): // to display a message in the Text Status GUIText object var DisplayGameStatus : GUIText; // determines if the game is paused by using a simple ON or OFF state set initially to OFF/0 var AreWePaused : int = 0; // functions that are run when the game play levels are first loaded and run function Start () { // when the level starts we hide the cursor and lock the cursor position to middle of screen // remember to show and unlock cursor when level ends and score needs to be submitted Time.timeScale = 1; Screen.lockCursor = true; Screen.showCursor = false; } function Update () { if (Input.GetButtonUp ("Pause")) { TogglePause(); } } function TogglePause() { if (AreWePaused == 0) { Time.timeScale = 0; AreWePaused = 1; DisplayGameStatus.guiText.text = "Paused"; } else if (AreWePaused == 1) { // Could just be an else Time.timeScale = 1; AreWePaused = 0; DisplayGameStatus.guiText.text = "Going"; } }
If your DisplayGameStatus GUI only ever shows "Paused" (like in Unity Invaders/Unitroids), then you can leave the text as "Paused" and keep the object deactivated normally, and do this: Code (csharp): function Update() { if (Input.GetButtonDown("Pause") ) { Time.timeScale = 1-Time.timeScale; DisplayGameStatus.active = !DisplayGameStatus.active; } } That just toggles timeScale from 1 to 0 and vice versa, and toggles DisplayGameStatus from inactive to active and vice versa. I'd be more inclined to use GetButtonDown for this sort of thing, and if you'd rather use lfrog's method, I think it's better practice to use booleans for areWePaused type variables (true/false instead of 1/0). Not that it would function differently, but it makes the code more readable and potentially less prone to bugs. --Eric
Thanks lfrog and Eric. This is what I'm after (it makes sense to a noob coder like me. I agree though that true/false is better than 1/0 but I only ever use 0 for OFF and 1 for ON so I rarely get confused. My problem thinking these sort of things through is that I mess up function Updates by always trying to throw in a yield statement (obviously not allowed) so forgetting that I can call a function within the function update to make things go. I must remember this. G'nite for now.