GUI Menu not working - Buttons not sending the right thing.

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?

Sorry! I can edit it! It's not reacting to my click. I know it's my setup of the GUI Box I believe. (:

It 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.

1 Answer

1

It could be a problem with your logic. Let’s try and simplify it, shall we? I can tell that the functionality here isn’t exaclty as you desire, but I can’t read your mind, so I can’t make it as you want it. :slight_smile:

Hopefully this is enough to get you moving in the right direction!

using UnityEngine;
using System.Collections;

public class PauseMenu : MonoBehaviour
{
	public enum GUIState
	{
		Normal,
		Paused,
		Journal
	}

	public GUISkin skin;
	public Texture blackTexture;
	public float alphaFadeValue = 0.4f;
	public GUITexture game;
	public string levelToLoad;

	private GUIState currentGUIState;
	
	void Awake(){
		game.enabled = false;
	}
	
	void Update()
	{
		if (Input.GetKeyDown (KeyCode.Escape))
			TransitionToState(GUIState.Paused);	
	}
	
	void OnGUI()
	{
		switch (currentGUIState)
		{
		case GUIState.Normal:
			alphaFadeValue = 0f;
			break;
		case GUIState.Journal:
			DrawJournalEntries();
			break;
		case GUIState.Paused:
			DrawPauseMenu ();
			alphaFadeValue = 0.5f;
			GUI.color = new Color (alphaFadeValue, alphaFadeValue, alphaFadeValue, alphaFadeValue);
			GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), blackTexture);
			break;
		}
	}

	void JournalEntries()
	{
		game.enabled = true;
	}

	void TransitionToState(GUIState state)
	{
		currentGUIState = state;

		switch (currentGUIState)
		{
		case GUIState.Normal:
			Time.timeScale = 1;
			Screen.showCursor = false;
			Screen.lockCursor = true;
			break;
		case GUIState.Paused:
			Time.timeScale = 0;
			Screen.showCursor = true;
			Screen.lockCursor = false;
			game.enabled = false;
			break;
		case GUIState.Journal:
			break;
		}
	}

	void DrawJournalEntries()
	{
		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 ();
	}

	
	void DrawPauseMenu()
	{
		GUILayout.BeginArea(new Rect ((Screen.width * 0.5f) - 100, (Screen.height * 0.5f) - 48, 200, 200));
		
		if (GUILayout.Button ("Resume"))
			TransitionToState(GUIState.Normal);
		
		if (GUILayout.Button ("Journal Entries"))
			JournalEntries();
		
		if (GUILayout.Button ("Journal Entries (Works)"))
			TransitionToState(GUIState.Journal);

		if (GUILayout.Button("Quit Game"))
			Application.Quit ();
		
		if (GUILayout.Button("Main Menu"))
			Application.LoadLevel ("levelToLoad");

		GUILayout.EndArea();
	}
	
}

My goodness! You're a genius! (: Thank you so much! This is EXACTLY what I was looking for! (: The MainMenu says that it needs to be added to my build settings, but it's already there. Do I need to add it again? Or...? (: Again, you're a genius!

Remove the quotes from the following line (108 above) Application.LoadLevel ("levelToLoad"); to => Application.LoadLevel (levelToLoad);

Thank you! (: I should have seen that! Thank you so much!!