- Home /
How to Hide the GUI when time.scale = 1 again
Hey guys, just a quick question, does anyone know how I could make my GUI dissapear again once the timescale is returned to 1, here's the pause script:
using UnityEngine;
using System.Collections;
public class Pause : MonoBehaviour {
public GUITexture movePause;
public bool gamePaused = false;
public GUISkin guiSkin;
Rect windowRect = new Rect (0, 0, 620, 520);
private bool toggleTxt = false;
string stringToEdit = "Text Label";
float hSliderValue = 0.0f;
float vSliderValue = 0.0f;
private float hSbarValuet;
private float vSbarValue;
private Vector2 scrollPosition = Vector2.zero;
void Start (){
Time.timeScale = 1.0f;
}
void Update (){
windowRect.x = (Screen.width - windowRect.width) / 2;
windowRect.y = (Screen.height - windowRect.height) / 2;
foreach (Touch touch in Input.touches) {
if (gamePaused == false) {
if (movePause.HitTest (touch.position)) {
gamePaused = true;
Time.timeScale = 0.0f;
} else {
gamePaused = false;
Time.timeScale = 1.0f;
}
}
}
}
void OnGUI(){
GUI.skin = guiSkin;
if (gamePaused){
windowRect = GUI.Window (0, windowRect, PauseMenu, "Paused");
}
}
void PauseMenu(int windowPause) {
if (GUI.Button ( new Rect(140,50,340,50), "Resume"))
gamePaused = false;
if (GUI.Button ( new Rect(140,120,340,50), "Restart"))
Application.LoadLevel("TestScene1");
GUI.Button ( new Rect(140,190,340,50), "Options");
if (GUI.Button ( new Rect(140,260,340,50), "Main Menu"))
Application.LoadLevel ("Menu");
if (GUI.Button ( new Rect(140,330,340,50), "Quit"))
Application.Quit ();
}
}
Thanks in advanced!
What GUI you're trying to hide? the pause menu? the code you have should do it.
Btw, the logic in your foreach loop is kind of weird, it looks redundant. $$anonymous$$aybe we could help your write it in a better way. what are you trying to? you touch a certain button and you pause/unpause?
Yeah it does look redundant doesn't it, but whenever I remove one of the elements controlling timescale, it all falls appart.
Yeah if you could help write it better that'd be great, but no it's not functioning how I want it to, because it works to a point, the GUI will all respond properly and when I hit resume it goes back to the game fine, but if the user presses anywhere that isn't the GUI interface, the game will resume, but the GUI will still be there
That means that if a user touches something outside of the GUI window, it allows them to kind-of cheat and accelerate (space shooter) without the ship moving until they hit resume.
Basically I just need to be able to stop that somehow
Answer by Scribe · Mar 18, 2014 at 10:19 AM
The problem is your for loop:
foreach (Touch touch in Input.touches) {
if(gamePaused == false){
if(movePause.HitTest(touch.position)) {
gamePaused = true;
Time.timeScale = 0.0f;
}else{
gamePaused = false;
Time.timeScale = 1.0f;
}
}
}
Once the game is paused if(gamePaused == false)
will never return true so you can never get the point in your for loop where you unpause again:
Maybe try doing this instead:
foreach (Touch touch in Input.touches) {
if(movePause.HitTest(touch.position)) {
gamePaused = !gamePaused;
Time.timeScale = 1.0f - Time.timeScale;
break;
}
}
on the first touch input it finds that makes movePause.HitTest(touch.position)
true, it will set gamePaused to 'not' gamePaused (basically a toggle) and set Time.timeScale
to 1-itself (toggles between 1 and 0).
Scribe
I haven't actually checked what movePause.HitTest(touch.position) does, so I'm assu$$anonymous$$g this part worked! :P
Thanks for the response, I've just tried that and when I hit pause, it sort-of flashes the pause menu twice before pausing or unpausing, and it still allows players to press my touch inputs, it does how-ever stop users from unpausing the game when touching anywhere that isnt my buttons or GUI
So the flickering can probably be fixed by doing:
foreach (Touch touch in Input.touches) {
if(touch.phase == TouchPhase.Began && movePause.HitTest(touch.position)) {
gamePaused = !gamePaused;
Time.timeScale = 1.0f - Time.timeScale;
break;
}
}
Regarding "and it still allows players to press my touch inputs" these are not based on timeScale so they will still work unless you check for gamePaused in their scripts.
Ok that works, although my resume button in the GUI won't unpause it now, though that shouldn't be to hard to figure out.
Ok, do you know how I could accomplish that? This is one of my movement scripts:
using UnityEngine;
using System.Collections;
public class $$anonymous$$oveUp : $$anonymous$$onoBehaviour {
public GUITexture moveUp;
// Update is called once per frame
void Update () {
foreach (Touch touch in Input.touches) {
if (moveUp.HitTest(touch.position)) {
{
// Apply a force
rigidbody.AddRelativeForce(-8,0,0);
//renderer.material.color = Color.red;
//GetComponent<ParticleEmitter>().emit = true; //<-- BUG - Causes ship to flicker and die. Odd.
GetComponent<ParticleRenderer>().enabled = true;
AudioSource audioSource = GetComponent<AudioSource>();
if (!audioSource.isPlaying)
audioSource.Play();
}
}
else
{
//GetComponent<ParticleEmitter>().emit = false;
GetComponent<ParticleRenderer>().enabled = false;
//GetComponent<ParticleEmitter>().ClearParticles();
//renderer.material.color = Color.white;
GetComponent<AudioSource>().Pause();
}
}
}
}
The subtraction of the time scale looks suspicious to me - shouldn't it be:
foreach (Touch touch in Input.touches) {
if(touch.phase == TouchPhase.Began && movePause.HitTest(touch.position)) {
gamePaused = !gamePaused;
Time.timeScale = gamePaused ? 0f : 1f;
break;
}
}
?
Even better extract that to a TogglePause method:
void TogglePause()
{
gamePaused = !gamePaused;
Time.timeScale = gamePaused ? 0f : 1f;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
GUI.HorizontalSlider not working 2 Answers
How to disable my Script and Gui 1 Answer
GUItexture change on mouseenter 3 Answers
In Game Credits? 1 Answer