- Home /
C# script works wrong ( GUI ) How to fix it ?
Hello everybody. I am working on puzzle game. On the first scene i have got menu : Play More Games Exit.
When i press "Play".This menu is closing and Difficulty menu should be open. It works.
When i press "MORE Games" it works only from second time. First time it opens Difficulty menu. But if i press second time "More Games" it works good and open "More Games menu.WHy??
When i press Exit it always opens More Games Menu. Why? I wrote this line of code in script Application.Quit ();
Then i have 1 more mistake.
Difficulty menu contains : Normal - when i press normal , normal games are opening Superior - when i press superior , AGAIN NORMAL GAMES ARE OPENING . WHY ?? Genius - works good. Back - works good.
I think it is a GUI problem maybe i could be wrong. Help me please to fix this problem please.
Can you post the scripts you're using that you're invoking on these buttons? If some are working and some aren't it sounds like a script or a configuration issue.
Answer by Legrandes · Mar 20, 2015 at 04:10 PM
i am a beginner in scripting. i wish everybody can understand what i wrote. if not i will explain everything.
i have gameobjects in hierachy not active. when i press on the button the right menu is opening.
public GameObject DifficultyMenu;
public GameObject moregamesmenu;
public GameObject normallevelmenu;
public GameObject superiorlevelmenu;
public GameObject glmenu;
public bool Play;
public bool difficulty;
public bool normal;
public bool moregames;
public bool exit;
public bool back;
public bool backn;
public bool backg;
public bool backs;
public bool home;
public float width;
public float height;
public float hm;
void Update () {
OnGUI ();
}
void OnGUI()
{
if (GUI.Button (new Rect (Screen.width / width, Screen.height / height * hm, Screen.width/4, Screen.height/2), textura, stil) )
{
knopka();
}
}
void knopka()
{
if (Play)
Application.LoadLevel (1);
if (difficulty) {
DifficultyMenu.SetActive (true);
MainMenu.SetActive (false);
}
if (normal)
{
chekstatusnormal();
}
if (superior)
{
chekstatussuperior();
}
if (moregames)
{
moregamesmenu.SetActive (true);
MainMenu.SetActive (false);
}
if (back) {
checkstatus ();
}
if (backn)
{
chekstatusabckn();
}
if (backs)
{
chekstatusabcks();
} if (backg)
{
chekstatusabckg();
}
if (exit)
{
Application.Quit ();
}
if (home)
{
Application.LoadLevel ("menu");
}
}
void checkstatus()
{
if (!MainMenu.activeInHierarchy && DifficultyMenu.activeInHierarchy)
MainMenu.SetActive (true);
DifficultyMenu.SetActive (false);
}
void chekstatusnormal()
{
if (!normallevelmenu.activeInHierarchy && DifficultyMenu.activeInHierarchy )
normallevelmenu.SetActive (true);
DifficultyMenu.SetActive (false);
}
void chekstatussuperior()
{
if (!superiorlevelmenu.activeInHierarchy && DifficultyMenu.activeInHierarchy )
superiorlevelmenu.SetActive (true);
DifficultyMenu.SetActive (false);
}
void chekstatusabckn()
{
if (!DifficultyMenu.activeInHierarchy && normallevelmenu.activeInHierarchy)
normallevelmenu.SetActive (false);
DifficultyMenu.SetActive (true);
}
void chekstatusabcks()
{
if (!DifficultyMenu.activeInHierarchy && superiorlevelmenu.activeInHierarchy)
superiorlevelmenu.SetActive (false);
DifficultyMenu.SetActive (true);
}
void chekstatusabckg()
{
if (!DifficultyMenu.activeInHierarchy && glmenu.activeInHierarchy)
glmenu.SetActive (false);
DifficultyMenu.SetActive (true);
}
It seems like it would be a problem related to when you set the variables you 'if check'. So when do you set 'moregames' or 'superior' to true?
Also just a note, you shouldn't need to call OnGUI() from Update() if you're using the base Unity functions.