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?

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;
}

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.