- Home /
GUI Window problem
Hi all,
i have worked with unity before six months. but am trying and working with GUI windows. it's giving trouble to me about 2 hours. here is my code
using UnityEngine;
using System.Collections;
public class Menu : MonoBehaviour {
public GUIStyle background = new GUIStyle();
private bool menus = true;
public Rect window1 = new Rect(3,3,Screen.width-3,Screen.height-3);
void OnGUI()
{
GUI.Label(new Rect(0,0,Screen.width,Screen.height)," ",background);
if(menus)
{
if(GUI.Button(new Rect((Screen.width)/2-50,(Screen.height)/2-60,100,30),"Play Game"))
{
Application.LoadLevel(1);
}
else if(GUI.Button(new Rect((Screen.width)/2-50,(Screen.height)/2-20,100,30),"Settings"))
{
window1 = GUI.Window(1,window1,settingsWindow,"");
menus = false;
}
else if(GUI.Button(new Rect((Screen.width)/2-50,(Screen.height)/2+20,100,30),"Credits"))
{
}
}
}
void settingsWindow(int windowID)
{
GUI.Label(new Rect(50,50,100,30),"Volume");
}
}
i have worked before in the same thing. here the problem is the new window is not opening. can any one say what am doing wrong?? Thank you in advance.
Answer by sriram90 · Sep 02, 2011 at 12:11 PM
I have called the window directly when click on button in my question. thats not the correct way. here we need to set a boolean value for that,if it is correct we need to open the window.
here is the correct code....
using UnityEngine;
using System.Collections;
public class Menu : MonoBehaviour {
public GUIStyle background = new GUIStyle();
private bool menus = true;
private bool settingsWin = false;
public Rect window1 = new Rect(3,3,Screen.width-3,Screen.height-3);
void OnGUI()
{
GUI.Label(new Rect(0,0,Screen.width,Screen.height)," ",background);
if(menus)
{
if(GUI.Button(new Rect((Screen.width)/2-50,(Screen.height)/2-60,100,30),"Play Game"))
{
Application.LoadLevel(1);
}
else if(GUI.Button(new Rect((Screen.width)/2-50,(Screen.height)/2-20,100,30),"Settings"))
{
settingsWin = true;
menus = false;
}
else if(GUI.Button(new Rect((Screen.width)/2-50,(Screen.height)/2+20,100,30),"Credits"))
{
}
}
if(settingsWin)
{
window1 = GUI.Window(1,window1,settingsWindow,"");
}
}
void settingsWindow(int windowID)
{
GUI.Label(new Rect(50,50,100,30),"Volume");
}
}
Answer by Jorn · Aug 27, 2011 at 04:35 PM
The problem here is that you call the settingsWindow function ones. After setting the menus boolean to false, the function never gets called again, therefore is never drawn on the screen again.
ya here we need to set a boolean value....we have to check if it is true means the window get open...
Your answer
Follow this Question
Related Questions
Gui in Gui Not Working ? 1 Answer
GUI Button sound problem, don´t work 1 Answer
What's a great way to make a custom information window pop up 1 Answer
GUI Rotation 1 Answer
How to make multiple GUI window? 1 Answer