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 21, 2014 at 12:00 AM · inputpause menu

GUI Skin Not Working With Static Bool

Hey guys, sorry about this but I really need to fix this, in my game I have a pause menu and I need to have it so when the game is paused, my touch buttons don't allow presses until unpaused.

I've tried doing this with GetComponent but when I do it that way the skin will show up, but I can never press the touch button even when not paused.

The way that works at the moment is using a static bool for gamePaused, but when doing it this way, my skin doesn't appear, just unity's basic GUI

 using UnityEngine;
 using System.Collections;
 
 public class Pause : MonoBehaviour {
     
     public GUITexture movePause;
     public static 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  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");
         if (GUI.Button ( new Rect(140,190,340,50), "Main Menu"))
             Application.LoadLevel ("Menu");
         GUI.Button ( new Rect(140,260,340,50), "Score: ");
         if (GUI.Button ( new Rect(140,330,340,50), "Quit"))
             Application.Quit ();
     }
 }

that's the code I've got for the pause script, the second I remove the 'static' part of the bool, the Skin will show, but then I can't disable my touch buttons on pause.

Please help?

Comment
Add comment · Show 1
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 S_Byrnes · Mar 21, 2014 at 05:01 AM 0
Share

Bump, please help me?

2 Replies

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

Answer by zharik86 · Mar 21, 2014 at 05:54 AM

Frankly speaking, never I met such situation. But generally there is an idea as to correct it. If you so much need to make a variable as static, I recommend to carry out it in a separate class. I will explain that I mean. Create a class as usual (Create-> C# Script). Let the name at it will be myPauseClass. It will look, as shown below:

  public class myPauseClass {
   public static bool gamePaused = false;
  }

Then, remove the gamePaused variable from your class. Also address now to such variable through a class, that is, for example:

  void  Update () {
   windowRect.x = (Screen.width - windowRect.width) / 2;
   windowRect.y = (Screen.height - windowRect.height) / 2;
 
   foreach (Touch touch in Input.touches) {
    if (myPauseClass.gamePaused == false) {
     if (movePause.HitTest (touch.position) && touch.phase == TouchPhase.Began) {
      myPauseClass.gamePaused = true;
      Time.timeScale = 0.0f;
     } else {
       myPauseClass.gamePaused = false;
       Time.timeScale = 1.0f;
     }
    }
   }
  }

Similarly make for other your functions.

Comment
Add comment · Show 15 · 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 S_Byrnes · Mar 21, 2014 at 07:48 AM 0
Share

Thanks for the response, that does the same as just a standard public static bool for me, everything works the way it should but just my GUISkin wont show up, only the default GUI..

 using UnityEngine;
 using System.Collections;
 
 public class Pause : $$anonymous$$onoBehaviour {
     
     public GUITexture movePause;
     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;
 
     public class myPauseClass {
         public static bool gamePaused = false;
     }
 
     void  Update () {
         windowRect.x = (Screen.width - windowRect.width) / 2;
         windowRect.y = (Screen.height - windowRect.height) / 2;
         
         foreach (Touch touch in Input.touches) {
             if (myPauseClass.gamePaused == false) {
                 if (movePause.HitTest (touch.position) && touch.phase == TouchPhase.Began) {
                     myPauseClass.gamePaused = true;
                     Time.timeScale = 0.0f;
                 } else {
                     myPauseClass.gamePaused = false;
                     Time.timeScale = 1.0f;
                 }
             }
         }
     }
     
     void OnGUI(){
         
         GUI.skin = guiSkin;
         
         if (myPauseClass.gamePaused){
             windowRect = GUI.Window (0, windowRect, Pause$$anonymous$$enu, "Paused");
         }
     }
     
     void Pause$$anonymous$$enu(int windowPause) {
         
         if (GUI.Button ( new Rect(140,50,340,50), "Resume"))
         {
             myPauseClass.gamePaused = false;
             Time.timeScale = 1.0f;
         }
         if (GUI.Button ( new Rect(140,120,340,50), "Restart"))
             Application.LoadLevel("TestScene1");
         if (GUI.Button ( new Rect(140,190,340,50), "$$anonymous$$ain $$anonymous$$enu"))
             Application.LoadLevel ("$$anonymous$$enu");
         GUI.Button ( new Rect(140,260,340,50), "Score: ");
         if (GUI.Button ( new Rect(140,330,340,50), "Quit"))
             Application.Quit ();
     }
 }

and here's my movement script incase you need that too:

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 (Pause.myPauseClass.gamePaused == false) {
                             if (moveUp.HitTest (touch.position)) {
                                     {
                                             // Apply a force
                                             rigidbody.AddRelativeForce (-8, 0, 0);
                                             //renderer.material.color = Color.red;
                                             //GetComponent<ParticleEmitter>().emit = true;
                                             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 zharik86 · Mar 21, 2014 at 06:41 PM 0
Share

@S_Byrnes It seems, class Pause works normally. Whether check in the Inspector the link to public the GUISkin variable is set.

avatar image S_Byrnes · Mar 22, 2014 at 01:14 AM 0
Share

I've been checking all over the place in the inspectors, and I don't think it's that because once I remove the static part, the skin will show up

avatar image zharik86 · Mar 22, 2014 at 09:32 AM 0
Share

@S_Byrnes So, excuse for a carelessness. I suggested to make the class myPauseClass an external class that it wasn't inherited from any other class. So it is necessary to delete the class myPauseClass created by you from the class Pause. And to make it external as I wrote above (it it shan't to be inherited from anybody). Try so. And I now once again will insert your code into Unity and I will check, but last time, it seems, worked normally. However, I took only two Update and OnGUI functions.

avatar image S_Byrnes · Mar 22, 2014 at 12:13 PM 0
Share

Ok, when I make a new C# script, what would I call it?

Show more comments
avatar image
0

Answer by sriram90 · Mar 21, 2014 at 05:54 AM

Hi @S_Byrnes,

I tried with unity editor and its working fine for me and nothing wrong with your static bool. Just try this instead of yours.

 Touch touch;
 if(Input.touchCount > 1) 
 {
  touch = Input.touches[0]; 
  if (gamePaused == false) 
  {
   if (movePause.guiTexture.HitTest (touch.position)) 
   {
     gamePaused = true;
     Time.timeScale = 0.0f;
   } 
  }
 }
     
 if (GUI.Button ( new Rect(140,50,340,50), "Resume"))
 {
  gamePaused = false;
  Time.timeScale = 1.0f;
 }
Comment
Add comment · Show 3 · 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 S_Byrnes · Mar 21, 2014 at 07:39 AM 0
Share

Thanks for the response, I tried it and it won't let me pause the game at all, and I still can't press my buttons ever..

Here's what I got using your script with $$anonymous$$e:

 using UnityEngine;
 using System.Collections;
 
 public class Pause : $$anonymous$$onoBehaviour {
     
     public GUITexture movePause;
     public static 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  Update (){
         
         windowRect.x = (Screen.width - windowRect.width) / 2;
         windowRect.y = (Screen.height - windowRect.height) / 2;
         
         Touch touch;
         if(Input.touchCount > 1) 
         {
             touch = Input.touches[0]; 
             if (gamePaused == false) 
             {
                 if (movePause.HitTest (touch.position)) 
                 {
                     gamePaused = true;
                     Time.timeScale = 0.0f;
                 } 
             }
         }
     }
     
     void OnGUI(){
         
         GUI.skin = guiSkin;
         
         if (gamePaused){
             windowRect = GUI.Window (0, windowRect, Pause$$anonymous$$enu, "Paused");
         }
     }
     
     void Pause$$anonymous$$enu(int windowPause) {
         
         if (GUI.Button ( new Rect(140,50,340,50), "Resume"))
         {
             gamePaused = false;
             Time.timeScale = 1.0f;
         }
         if (GUI.Button ( new Rect(140,120,340,50), "Restart"))
             Application.LoadLevel("TestScene1");
         if (GUI.Button ( new Rect(140,190,340,50), "$$anonymous$$ain $$anonymous$$enu"))
             Application.LoadLevel ("$$anonymous$$enu");
         GUI.Button ( new Rect(140,260,340,50), "Score: ");
         if (GUI.Button ( new Rect(140,330,340,50), "Quit"))
             Application.Quit ();
     }
 }

and if it helps, here is one of the 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 (Pause.gamePaused = false) {
                                 if (moveUp.HitTest (touch.position)) {
                                         {
                                                 // Apply a force
                                                 rigidbody.AddRelativeForce (-8, 0, 0);
                                                 //renderer.material.color = Color.red;
                                                 //GetComponent<ParticleEmitter>().emit = true;
                                                 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 sriram90 · Mar 24, 2014 at 05:28 AM 0
Share

@S_Byrnes,

Its Really strange man .. Eventhough i sent the code after it's worked well for me.

avatar image zharik86 · Mar 24, 2014 at 07:06 AM 0
Share

@sriram90 I completely agree that is very strange. I also checked all code and everything works for me perfectly. If there is a certain error, or it is any bug of Unity, or somewhere in other scripts GUISkin is nullified. In the comments I wrote a test project which shall work for 100%. I will look at continuation: )

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

Pause Menu won't open... 1 Answer

Pause Menu Wont Disable Mouse,Pause menu wont disable Player Controls 0 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Game dosen't pause 1 Answer

Trigger 'if' statement while in TextField 2 Answers


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