How to show GUI Groups on button click event?

Hi guys,

Im trying to show a groupbox message when I click in one button…

I have 2 classes… my “main” class… e the other one named “alert”.

The problem is: if I put the dialog.showDialog("test"); on button event … this doesnt work :frowning:

But if I just put the dialog.ShowDialog(“test”); outside of Button click… when my game starts I can see the box.




using UnityEngine;
using System.Collections;

public class Main : MonoBehaviour {

	public Alert dialog = new Alert();
	public Texture bg;
	public GUIStyle btLogin;
	public GUIStyle fieldUsername;
	public string username;
	public string pass;
	void Start() {
		username = "test";
		pass = "";
	void Update() {
	void OnGUI() {		
		// Background	
		GUI.DrawTexture(new Rect(0, 0, 965, 675), bg);
		if (GUI.Button(new Rect(686, 563, 219, 69), "LOGIN") {
			if (username == "") {
			} else {
		username = GUI.TextField(new Rect(685, 445, 220, 30), username, 50, fieldUsername);
		pass = GUI.PasswordField(new Rect(685, 492, 220, 30), pass, "*"[0], 50, fieldUsername);



using UnityEngine;
using System.Collections;

public class Alert {

	// Use this for initialization
	void Start () {
	// Update is called once per frame
	void Update () {
	public void showDialog(string msg) {
		GUI.BeginGroup(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 100));

		GUI.Box(new Rect(0, 0, 150, 150), "");
		GUI.Label(new Rect(10, 40, 80, 30), msg);

		GUI.EndGroup ();

GUI functions must be called every frame. When you put the ShowDialog in a button event, it is called that frame and your gui is displayed for one frame. The reason you can see it when you put it outside the button click is because it is called every frame then.

When the button is clicked, set a boolean to true. In the same OnGUI function, after the button call, check that boolean (which will be true if the button was pressed) and call showDialog if true.

If you want to make the dialog box last X seconds, start a coroutine when button is clicked, instead of setting the dialog.

Example coroutine:

function DialogTimer(float dialogDuration)
isDialogBoxVisible = true;
yield return new WaitForSeconds(dialogDuration);
isDialogBoxVisible = false;

I don’t use javascript so my synthax for defining a coroutine might be incorrect, but
I hope you get the idea.