- Home /
GUILayout not working for me C#
Hello, I've coded the following OnGUI function. My Debug.Log methods are logging properly, showing that the code is getting reached. However, my GUI isn't showing up! Why is this?
void OnGUI() {
if (ShowRestartWindow) {
#region Restart Window
if (Event.current.type == EventType.Layout) {
Debug.Log("layout");
GUILayout.BeginArea(new Rect(10f, 10f, 100f, 100));
{
Debug.Log("RestartWindow shown");
PlayerController pc = GetComponent<PlayerController>();
int playerScore = pc.playerDisplayedScore;
int highScore = pc.HighestPlayerScore;
GUILayout.Label("Your Score: " + playerScore.ToString());
GUILayout.Label("High Score: " + highScore.ToString());
if (GUILayout.Button("Play Again?")) {
//Time.timeScale = 1;
Application.LoadLevel("main");
}
}
GUILayout.EndArea();
}
#endregion
}
}
Try drop your rect back to (0,0,100,100).
Any luck? Worked for the last person who had this problem on UA
Answer by Ngoc Ngo · Jul 08, 2014 at 03:54 AM
I think it would work without the guard on line 5.
This should be a comment, not an answer.
Also: It didn't work due to GUI $$anonymous$$anipulation rules, that's why they're there in the first place.
@phxvyper: No, this should be an answer since it is correct. What "GUI $$anonymous$$anipulation rules" are you talking about? OnGUI is called to process events. There are many different type of events. The layout event is only for gathering information about the controls you want to draw. The controls itself are drawn when the repaint event is issued. Input is processed when a mouse keyboard event is issued. Since you filter out all events but layout the whole system doesn't work.
You might want to read my crash course about the GUI system.
@Bunny83 It isn't correct for my situation. I was manipulating a variable with affected the state of the GUI (somehow), and it caused an error to pop up. I googled the error and another person who had the issue posted it on unity answers. Someone replied with that if statement, the error is now gone because of it.
I used the repaint event in the if statement and it still showed the error.
Edit: I changed the GUI around a bit and removed that if statement. The changes i made didn't work until the if statement was removed. I'm unsure what exactly was creating that error. ~ so now it works
Your answer
Follow this Question
Related Questions
My OnGUI() Won't show the Button elements :( 0 Answers
Drawing several BeginArea inside a BeginScrollview (GUILayout) produces an unexpected behaviour 1 Answer
How can I enable/disable a script or a method in a script from another editorwindow script ? 0 Answers
How to fix ArgumentException? 3 Answers