- Home /
Head Scratcher for GUILayout.textfield
This makes no sense. I'm setting up forms for a 2d app to be stored in the asset database, but every time I use GUILayout.textfield the way I have used it in editor scripts many multiple times, I get an error.
string bandName;
inside a function:
bandName=GUILayout.TextField("Item Name: " , bandName);
Here is the error: ArgumentNullException: Argument cannot be null. Parameter name: key System.Collections.Generic.Dictionary`2[System.String,UnityEngine.GUIStyle].TryGetValue (System.String key, UnityEngine.GUIStyle& value) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:588) UnityEngine.GUISkin.FindStyle (System.String styleName) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GUISkinBindings.gen.cs:275) UnityEngine.GUISkin.GetStyle (System.String styleName) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GUISkinBindings.gen.cs:258) UnityEngine.GUIStyle.op_Implicit (System.String str) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GUIStyleBindings.gen.cs:819) GUI.ChangeBandName () (at Assets/Scripts/GUI.cs:204) GUI.DetailView () (at Assets/Scripts/GUI.cs:107) GUI.OnGUI () (at Assets/Scripts/GUI.cs:66)
ArgumentException: Getting control 0's position in a group with only 0 controls when doing Repaint Aborting UnityEngine.GUILayoutGroup.GetNext () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GUILayoutUtilityBindings.gen.cs:511) UnityEngine.GUILayoutUtility.BeginLayoutGroup (UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options, System.Type LayoutType) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GUILayoutUtilityBindings.gen.cs:208) UnityEngine.GUILayout.BeginHorizontal (UnityEngine.GUIContent content, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GUILayoutBindings.gen.cs:235) UnityEngine.GUILayout.BeginHorizontal (UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GUILayoutBindings.gen.cs:227) GUI.OnGUI () (at Assets/Scripts/GUI.cs:64)
Line 64: GUILayout.BeginHorizontal(); in the OnGUI function Three lines later, I close it. GUILayout.EndHorizontal();
If I comment out the textfield line, it works. Anyone make head or tales of this? Thanks!
Answer by Jessespike · Jun 30, 2015 at 08:50 PM
The string is null as described in the cryptic error. GUILayout might need to be EditorGUILayout as well.
string bandName = "";
bandName = EditorGUILayout.TextField("Item Name: " , bandName);
Thanks Jessespike- if this is not going to be used in a custom editor/inspector, what should be used?
Answer by Bunny83 · Jul 01, 2015 at 12:03 AM
Well, just take a look at the description of the TextField. A Text field doesn't have a caption / label as it's editor version has. A GUILayout.TextField is just a textfield. Your problem is that your first string is used as "text" and the second one as GUIStyle as there is an implicit cast from string to GUIStyle. Since thestring you pass seems to be null / not initialized it throws an error when it tries to resolve the GUIStyle.
If you need a label for your text field you have to use a seperate GUILayout.Label
.
ps: The EditorGUI / EditorGUILayout class is only available in the editor. It's just an extension of the runtime classes GUI and GUILayout. In the editor you can use whatever function suits your need. However runtime scripts can only use GUI and GUILayout
Thanks Bunny! Guess it's back to the classic rects. Was hoping to get away from that.
@Gilead7: Huh? Why? You can still use GUILayout. If you want something like the editor version do something like this:
public static string LabeledTextField(string aLabel, string aText)
{
GUILayout.BeginHorizontal();
GUILayout.Label("Your label:", GUILayout.$$anonymous$$axWidth(100));
aText = GUILayout.TextField(aText);
GUILayout.EndHorizontal();
return aText;
}
This is just an example and it's not very customizable in it's current form. However you can make the style a parameter as well, you just need a lot overloads unless you always provide all needed info.
$$anonymous$$eep in $$anonymous$$d that you can create even your own cusom GUI components. If you're interested in that kind of development you should get ILSpy (if you don't have it already) and take a look at how the TextField is implemented in the EditorGUILayout namespace (UnityEditor.dll) and the GUILayout namespace (Unityengine.dll). Just as reference my GUI crash course ^^.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
TextField does not show up 2 Answers
Very simple - Appending a space to GUILayout.Label (C#) 2 Answers
How to fix ArgumentException? 3 Answers