- Home /
Chat script works in editor but not in stand alone.
Hey all,
So I have a bit of an interesting problem. I've not run in to anything like this before so hopefully someone will figure out why this is happening.
Anyway, I am trying to set up a chat window. When you hit enter/return, it will open the chat, you can then type your message and press enter/return again to send the message. The code I have below works in the Unity editor. As soon as I make an .exe version, the script opens the chat, then quickly closes the chat before I can type anything. If I'm fast I can type a letter before it closes. If I type a letter fast enough, the window stays open and functions as expected.
Here is my code:
if(chatOpen)
{
GUILayout.BeginArea( new Rect(10 ,Camera.main.pixelHeight - 125, 300, 50));
GUILayout.BeginHorizontal();
GUILayout.BeginVertical(GUILayout.Width(300));
GUI.SetNextControlName("myMessage");
myMessage = GUILayout.TextField(myMessage);
if(textControl)
{
print("textControl triggered");
GUI.FocusControl("myMessage");
textControl = false;
}
GUILayout.EndVertical();
GUILayout.EndHorizontal();
GUILayout.EndArea();
if(Event.current.keyCode == KeyCode.Return && chatSend > .1f)
{
print("keyCode Event Triggered");
GUI.SetNextControlName("");
GUI.FocusControl("");
sendNow = true;
}
}
The chatSend float was how I got it to work in the editor. Basically, you press enter, then it waits 0.1 seconds before you can press enter again to send the message. This prevents the chat opening and then closing right after it opens because OnGUI runs more than once per frame. I am receiving no errors when I run this and if I increase the time for chatSend from 0.1f to something like 0.3f it will wait 0.3 seconds and then close the chat window.
If you have any ideas why this is happening I'd really appreciate it.
Thanks!
Answer by drex150 · Aug 03, 2014 at 06:41 PM
I found an answer to this after some testing. Here is my code.
if(chatOpen)
{
if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return)
{
print("keyCode Event Triggered");
GUI.SetNextControlName("");
GUI.FocusControl("");
sendNow = true;
}
if(Camera.main != null)
{
GUILayout.BeginArea( new Rect(10 ,Camera.main.pixelHeight - 125, 300, 50));
GUILayout.BeginHorizontal();
GUILayout.BeginVertical(GUILayout.Width(300));
GUI.SetNextControlName("myMessage");
myMessage = GUILayout.TextField(myMessage);
if(textControl)
{
print("textControl triggered");
GUI.FocusControl("myMessage");
textControl = false;
}
GUILayout.EndVertical();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
}
I moved over event.current.keycode and added event.current.type = eventtype.keydown. I also moved this all above the text box's creation. This worked perfectly as such.
Answer by shock2provide · Aug 01, 2014 at 08:16 PM
Try Input.GetKeyDown(KeyCode.Return)
instead of Event.current.keyCode == KeyCode.Return
.
This checks the KeyPress only once per frame.
Regards
Marc
Because I'm changing the FocusControl to the text box, using Get$$anonymous$$eyDown will not do anything. The only way I've found to work is what I have currently. Any other method for checking if a button is pressed simply doesn't do anything.
Your answer
Follow this Question
Related Questions
Type UnityEngine does not contain a definition for RPC 1 Answer
How to have both, a scroll view AND vertical Layout? 1 Answer
Accessing local system ( File Browser ) 2 Answers
C# help in walking and jumping 1 Answer
Creating a Quest system 2 Answers