- Home /
ArgumentException: getting control 2's position in a group with only ... Gui.Layout issue.
I am building a chat program for my game and everything is working fine but I am getting this error and I can seem to remove it. here is my script:
private void OnGUI()
{
if (!Client.isConnected)
return;
Event e = Event.current;
if (e.type == EventType.keyDown && e.keyCode == KeyCode.Return)
{
if (GUI.GetNameOfFocusedControl() == "chat_input") // if you are focusing on the text field
{
SendMessage();
currentMessage = "";
Invoke("ScrollToBottom", 0.15f);
GUI.FocusControl(""); // remove focus from textfield
}
else if (GUI.GetNameOfFocusedControl() != "chat_input") // if you are NOT focusing on the text field
{
GUI.FocusControl("chat_input"); // focus on the textfield
}
}
if (chatHistory.Count > 50)
{
chatHistory.RemoveAt(0);
}
LoadGui();
}
private void LoadGui()
{
GUILayout.BeginArea(new Rect(0, Screen.height - 255, 450, 222));
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(450), GUILayout.Height(222));
foreach (var chatMessage in chatHistory)
{
if (chatMessage.StartsWith("ThisIsATell"))
{
string msg = BuildMessage(chatMessage);
GUI.skin = tellSkin;
GUILayout.Label(msg, GUILayout.MaxWidth(450));
}
else if (chatMessage.StartsWith("ThisIsAServerMessage"))
{
string msg = BuildMessage(chatMessage);
GUI.skin = server_messages_skin;
GUILayout.Label(msg, GUILayout.MaxWidth(450));
}
else if (chatMessage.StartsWith("ThisIsANormalMessage"))
{
string msg = BuildMessage(chatMessage);
GUI.skin = normalSkin;
GUILayout.Label(msg, GUILayout.MaxWidth(450)); // this is the line Unity complains about
}
}
GUILayout.EndScrollView();
GUILayout.EndArea();
GUI.SetNextControlName("chat_input");
currentMessage = GUI.TextField(new Rect(0, Screen.height - 25, 450, 25), currentMessage);
currentMessage = Regex.Replace(currentMessage, @"[^a-zA-Z0-9/!?-_ ]", "");
}
Hope someone knows what wrong here. any help is appreciated.
thanks.
Still a bit stumped by this, I have learned that it has something to do with changing layout elements during the keydown event
What is the full exception you get? Can you drop that here for us? Another thing I noticed while going through your code: 1. In your else branch you don't need to check for GUI.GetNameOfFocusedControl() != "chat_input" if your first condition already covers GUI.GetNameOfFocusedControl() == "chat_input". 2. I'm not 100% sure if C# is perfor$$anonymous$$g equally well with comparing string objects by using "==" operator, but I recommend you to use equals(string yourString), so it would be GUI.GetNameOfFocusedControl().equals("chat_input").
ArgumentException: Getting control 2's position in a group with only 2 controls when doing keyDown Aborting.
@AdmiralThrawn So when I activate the keydown event I get the execption. I cant see what it is I am changing in that call. im a bit confused as to why something is getting changed between the 2 calls.
I read this thread thoroughly still cant find my issue:
Your answer
![](https://koobas.hobune.stream/wayback/20220612132930im_/https://answers.unity.com/themes/thub/images/avi.jpg)