I have been looking for hours on the unityAnswers, the reference pages. I just can’t seem to figure it out.
All right, What I’m trying to do (originally) was to create a GUITexture when you click a menu button, which I did! And it works well. But then I wanted to write a script that when you click on the GUITexture it would open up a GUI Box (I believe that’s what it was called). That wouldn’t get called for some reason… But oh well! I’ll fix it later.
I came up with another button so that when you click this it would open up a GUI Box, to hold notes as buttons. The box wouldn’t get called. I just started working with GUI so I may be missing some code somewhere. Thanks if you can help! (:
using UnityEngine;
using System.Collections;
public class PauseMenu : MonoBehaviour {
public GUISkin skin;
public bool paused = false;
public Texture blackTexture;
public float alphaFadeValue = 0.4f;
public GUITexture game;
public string levelToLoad;
void Awake(){
game.enabled = false;
}
void Update(){
if (paused) {
Time.timeScale = 0;
Screen.showCursor = true;
Screen.lockCursor = false;
} else {
Time.timeScale = 1;
Screen.showCursor = false;
Screen.lockCursor = true;
}
if (Input.GetKeyDown (KeyCode.Escape)) {
paused = !paused;
game.enabled = false;
}
}
void OnGUI(){
if (paused) {
Pause ();
alphaFadeValue = 0.5f;
GUI.color = new Color (alphaFadeValue, alphaFadeValue, alphaFadeValue, alphaFadeValue);
GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), blackTexture);
}
else {
alphaFadeValue = 0f;
}
}
void Pause(){
GUILayout.BeginArea(new Rect ((Screen.width * 0.5f) - 100, (Screen.height * 0.5f) - 48, 200, 200));
if (GUILayout.Button ("Resume")) {
paused = !paused;
if (game.enabled == true) {
game.enabled = false;
}
}
if (GUILayout.Button ("Journal Entries")) {
game.enabled = true;
}
if (GUILayout.Button ("Journal Entries (Works)")) {
GUI.BeginGroup (new Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 100));
// All rectangles are now adjusted to the group. (0,0) is the topleft corner of the group.
// We'll make a box so you can see where the group is on-screen.
GUI.Box (new Rect (0,0,100,100), "Group is here");
GUI.Button (new Rect (10,40,80,30), "Click me");
// End the group we started above. This is very important to remember!
GUI.EndGroup ();
}
if (GUILayout.Button("Quit Game")) {
Application.Quit ();
}
if (GUILayout.Button("Main Menu")) {
Application.LoadLevel ("levelToLoad");
}
GUILayout.EndArea();
}
}
Your question is less direct/clear than it could be. Are you having a problem with buttons not appearing on screen or the wrong actions being taken when you press them?
– iwaldropSorry! I can edit it! It's not reacting to my click. I know it's my setup of the GUI Box I believe. (:
– WaterMinecraftIt was working perfectly as Unity intended. The problem was that a GUI Button only returns true for the frame that it is clicked (even then it's not a full frame, but a sub, evaluation, frame of the GUI system). I've posted an answer below which addresses the core issue, and makes some functional improvements that may simplify how you manage your GUI.
– iwaldrop