- Home /
Enter not being called
Hello! I am trying to get my chat message to be sent when the player has entered something into the textbox and presses enter, but for some odd reason Unity does not recognise when I press Enter/Return (I have done a debug on the if(Message != null) to make sure that it should run). It would be much appreciated if someone could help me on this matter!
void OnGUI()
{
if(GUIManager.Instance.Chatting == true)
{
GUIManager.Instance.isBusy = true;
GUI.SetNextControlName("MessageControl");
Message = GUI.TextArea(new Rect(Screen.width * (0.1f/10f), Screen.height * (0.112f/0.129f), Screen.width * (0.5f/2.3f), Screen.height * (0.3f/6.3f)), Message, 25);
GUI.FocusControl("MessageControl");
if(Message != null)
{
if(Input.GetKeyDown("enter") || Input.GetKeyDown("return"))
{
Debug.Log("Clicked Enter");
if(Message != null || Message != "")
{
GUIManager.Instance.Send_Public_AddChatMessage(NetworkManager.Instance.PlayerName + ": " + Message);
Message = "";
GUIManager.Instance.Chatting = false;
GUIManager.Instance.isBusy = false;
}
else
{
GUIManager.Instance.Chatting = false;
GUIManager.Instance.isBusy = false;
}
}
}
}
}
You should do your Input and $$anonymous$$ey presses in Update. Leave OnGUI() for showing the GUI and interacting with GUI elements (buttons, labels, etc).
Answer by fifthknotch · Mar 05, 2014 at 04:44 PM
Right now your GUI TextArea views "enter" as just another character. You need to specify a keyboard event to check if a specific key was pressed.
More info found here: http://docs.unity3d.com/Documentation/ScriptReference/Event.KeyboardEvent.html
They are not. GUI text area overrides Get$$anonymous$$ey. Check out this unity forum. they have the same issue: http://forum.unity3d.com/threads/69361-GUI-TextField-submission-via-quot-return-quot-key
if(e.keyCode == $$anonymous$$eyCode.Return || e.keyCode == $$anonymous$$eyCode.$$anonymous$$eypadEnter)
{
Debug.Log("Clicked Enter");
if($$anonymous$$essage != null || $$anonymous$$essage != "")
{
GUI$$anonymous$$anager.Instance.Send_Public_AddChat$$anonymous$$essage(Network$$anonymous$$anager.Instance.PlayerName + ": " + $$anonymous$$essage);
$$anonymous$$essage = "";
GUI$$anonymous$$anager.Instance.Chatting = false;
GUI$$anonymous$$anager.Instance.isBusy = false;
}
else
{
GUI$$anonymous$$anager.Instance.Chatting = false;
GUI$$anonymous$$anager.Instance.isBusy = false;
}
}
Works perfectly thank you guys!
Answer by perchik · Mar 05, 2014 at 04:34 PM
Try
Input.GetKeyDown(KeyCode.Return)
Nope! I think the real problem is that Unity thinks I am chatting and therefore not recognising the button press as a command. When I press enter, it merely goes a line down.
Ah! Then, @Fifth$$anonymous$$notch's solution below is what you want.
Your answer
Follow this Question
Related Questions
Enter/Exit Vehicle with the same key? 2 Answers
GetKeyDown code turned into a int issue [SOLVED] 1 Answer
How to play animation after pressing return 3 times ? 1 Answer
Having GetButton act like GetKeyDown 1 Answer
Trying to see how long the player holds down a key. GetKeyDown & GetKeyUp behaving oddly. (C#) 2 Answers