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!

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);

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