I have a script that contains all the methods for button presses as well as a Pause function that is called when escape is pressed from another script. I had it working fine last night, but then I tweaked it a bit, added a slider and now it’s giving me trouble. The crosshair image is set inactive as it should and Time.timeScale = 0 works fine but the buttons (Resume,Options, ExitToMainMenu) do not get set as active. I’m not receiving Null reference exceptions and I’ve used Debug.Log () to ensure the GameObject variables are being assigned properly.
Here is the entire Script:
Public Static Void Pause () is the method I need help with:
using UnityEngine;
using System.Collections;
using System;
public class Buttons : MonoBehaviour {
static GameObject player;
static GameObject canvas;
static GameObject play;
static GameObject options;
static GameObject exit;
static GameObject map;
static GameObject jungle;
static GameObject desert;
static GameObject tundra;
static GameObject swamp;
static GameObject crosshair;
static GameObject resume;
static GameObject exitToMainMenu;
static GameObject grassDensity;
void Start () {
canvas = GameObject.Find ("Canvas");
if (Application.loadedLevel == 0) {
MainMenuButtons ();
} else if (Application.loadedLevel == 1) {
SwampButtons ();
}
}
void MainMenuButtons () {
play = canvas.transform.FindChild ("Play").gameObject;
options = canvas.transform.FindChild ("Options").gameObject;
exit = canvas.transform.FindChild ("Exit").gameObject;
map = canvas.transform.FindChild ("Map").gameObject;
jungle = canvas.transform.FindChild ("Jungle").gameObject;
desert = canvas.transform.FindChild ("Desert").gameObject;
tundra = canvas.transform.FindChild ("Tundra").gameObject;
swamp = canvas.transform.FindChild ("Swamp").gameObject;
}
void SwampButtons () {
crosshair = canvas.transform.FindChild ("Crosshair").gameObject;
resume = canvas.transform.FindChild ("Resume").gameObject;
options = canvas.transform.FindChild ("Options").gameObject;
exitToMainMenu = canvas.transform.FindChild ("ExitToMainMenu").gameObject;
resume.gameObject.SetActive (false);
options.gameObject.SetActive (false);
exitToMainMenu.gameObject.SetActive (false);
}
public void Play () {
play.SetActive (false);
options.gameObject.SetActive (false);
exit.gameObject.SetActive (false);
map.gameObject.SetActive (true);
jungle.gameObject.SetActive (true);
desert.gameObject.SetActive (true);
tundra.gameObject.SetActive (true);
swamp.gameObject.SetActive (true);
}
public void Swamp () {
Application.LoadLevel ("Swamp");
}
public static void Pause () {
crosshair.gameObject.SetActive (false);
resume.gameObject.SetActive (true);
options.gameObject.SetActive (true);
exitToMainMenu.gameObject.SetActive (true);
Time.timeScale = 0;
}
public void Resume () {
}
public void Options () {
}
public void GrassDensity (float sliderValue) {
Terrain.activeTerrain.detailObjectDensity = sliderValue;
}
}