Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by S_Byrnes · Mar 18, 2014 at 09:22 AM · guipausehide

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Mar 18, 2014 at 09:37 AM 0
Share

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?

avatar image S_Byrnes · Mar 18, 2014 at 10:07 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 17 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scribe · Mar 18, 2014 at 10:20 AM 0
Share

I haven't actually checked what movePause.HitTest(touch.position) does, so I'm assu$$anonymous$$g this part worked! :P

avatar image S_Byrnes · Mar 18, 2014 at 10:27 AM 0
Share

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

avatar image Scribe · Mar 18, 2014 at 10:35 AM 0
Share

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.

avatar image S_Byrnes · Mar 18, 2014 at 10:41 AM 0
Share

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();
             }
     }
 }
 }
avatar image vexe · Mar 18, 2014 at 11:27 AM 0
Share

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;
 }
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges