- Home /
The question is answered, right answer was accepted
2 dialogs not displayable at the same time.
Hello !
It took me some time to understand that there are two calls on OnGUI method by frame and what to do to avoid weird problems. But it seems like I didn't quite understand all of it ...
My script handling the chat is like :
function OnGUI() {
if (Event.current.type == EventType.Layout) windowChat = GUI.Window (0, windowChat, GlobalChatWindow, "Chat");
}
function GlobalChatWindow (id : int) {
scrollPosition = GUILayout.BeginScrollView (scrollPosition);
for (var entry : ChatEntry in entries)
{
GUILayout.BeginHorizontal();
GUILayout.Label(entry.text);
GUILayout.FlexibleSpace ();
GUILayout.EndHorizontal();
GUILayout.Space(3);
}
// End the scrollview we began above.
GUILayout.EndScrollView ();
if (Event.current.type == EventType.keyDown && Event.current.character == "\n" && inputField.Length > 0)
{
inputField = myNickname + ": " + inputField;
ApplyGlobalChatText(inputField, 1);
networkView.RPC("ApplyGlobalChatText", RPCMode.Others, inputField, 0);
inputField = "";
}
GUI.SetNextControlName("Chat input field");
inputField = GUILayout.TextField(inputField);
}
And my script handling my score dialog is like :
function OnGUI() {
if (enableScoreDlg)
{
scoresDlg = GUI.Window(0, Rect (0.1*Screen.width, 0.1*Screen.height, 0.8*Screen.width, 0.8*Screen.height), ScoreWindow, "", scoreSkin.window);
}
}
with enableScoreDlg true only when the user uses the "tab" key. My problem now is that there is never both the windows together. If the user presses "tab", the chat window disappears and my score dialog appears, when he releases it, my score dialog disappears and the chat reappears... What is wrong with my code ? I think this has to do with the 2 calls of OnGUI method every frame, and since the two dialogs are in different scripts there is a problem :(
Answer by PAEvenson · Dec 19, 2012 at 03:20 PM
My first guess would be when you are hitting the Tab button your Event.current.Type is getting changed, turning off your chat window.
OnGUI will be called on all ENABLED scripts on ACTIVE gameobjects each frame, if not multiple times per frame.
The 2nd thing I would check is after you hit the Tab button, pause the game and make sure the gameobjects and scripts and enabled/active.
The game object attaching the script is active and the script is enabled when I press the "tab" button.
and Event.current.type == EventType.Layout when you hit tab?
What do you suggest ? that I add this condition for the chat dialog?
aha! I found it! Both your windows have the same window ID. change one of the windows to ID 1
Answer by criptoonita · Dec 19, 2012 at 07:54 PM
I didn't see when you put true the enableScoreDlg, but maybe this idea would help.
In one of the two scripts when able to show dialog put this variable in a static class and static public bool. And then the other script read this static bool.
I'm sorry, I validated an answer but didn't close the question in case of someone had any clue for my last question.
$$anonymous$$y boolean variable is set in my Update() method when the user pushes the "tab" button. This was implicit with the intels I gave, but not obvious :). Thanks anyway for trying to help !
@$$anonymous$$iraSensei, you don't want to close the question after it's answered. Closing is generally reserved for duplicate or off topic questions.
Ah, when we close a question, there are multiple choices, and one of them is : the question has been answered and right answer was accepted. So I assumed that was the thing to do when I consider nothing interesting can be added.
Not sure why that is an option when closing. But accepting one of the answers is usually the last thing to do - that leaves it open for others to search for and find an answer if they have a similar problem. Closing it for when there's some sort of problem with the question itself.
Follow this Question
Related Questions
How to force Unity to Repaint an EditorWindow? 1 Answer
How completly remove GUI.Repaint() calls ? 1 Answer
Optimizing OnGUI - Too many gui elements? 2 Answers
iPhone Touch Screen? 2 Answers