Question by
miguellahoz · Aug 14, 2017 at 03:53 PM ·
editoreditorwindowwindowcustom inspector
Problem with EditorGUILayout.ObjectField
Hi!
I'm trying to make a custom window for Unity, and I need to make an Sprite Array selection.
I figured out all the code, but I have an error in a line.
The code is:
aux = (Sprite)EditorGUILayout.ObjectField(i.ToString(), data.dialogue.images[i], typeof(Sprite), false);
i is an int (for loop).
It launch an exception:
at UnityEngine.GUIUtility.ExitGUI () [0x00007] in C:\buildslave\unity\build\Runtime\IMGUI\Managed\GUIUtility.cs:163
at UnityEditor.EditorGUI.DoObjectField (Rect position, Rect dropRect, Int32 id, UnityEngine.Object obj, System.Type objType, UnityEditor.SerializedProperty property, UnityEditor.ObjectFieldValidator validator, Boolean allowSceneObjects, UnityEngine.GUIStyle style) [0x002ff] in C:\buildslave\unity\build\Editor\Mono\GUI\ObjectField.cs:169
at UnityEditor.EditorGUI.DoObjectField (Rect position, Rect dropRect, Int32 id, UnityEngine.Object obj, System.Type objType, UnityEditor.SerializedProperty property, UnityEditor.ObjectFieldValidator validator, Boolean allowSceneObjects) [0x00012] in C:\buildslave\unity\build\Editor\Mono\GUI\ObjectField.cs:21
at UnityEditor.EditorGUI.ObjectField (Rect position, UnityEngine.Object obj, System.Type objType, Boolean allowSceneObjects) [0x00020] in C:\buildslave\unity\build\Editor\Mono\EditorGUI.cs:3148
at UnityEditor.EditorGUILayout.ObjectField (UnityEngine.Object obj, System.Type objType, Boolean allowSceneObjects, UnityEngine.GUILayoutOption[] options) [0x00018] in C:\buildslave\unity\build\Editor\Mono\EditorGUI.cs:6853
at EditDialogueWindow.DrawSprites () [0x00081] in C:\Users\Miguel\Desktop\Pruebas 2017\Assets\Rivendel\Sistema dialogo\DialogueEditor.cs:283
at EditDialogueWindow.Draw () [0x0003e] in C:\Users\Miguel\Desktop\Pruebas 2017\Assets\Rivendel\Sistema dialogo\DialogueEditor.cs:269
at EditDialogueWindow.OnGUI () [0x00003] in C:\Users\Miguel\Desktop\Pruebas 2017\Assets\Rivendel\Sistema dialogo\DialogueEditor.cs:252
I don't know whats happening, because it's only a ObjectField call (the line in the error is the line I copied early).
Thanks for your help!
Comment
Answer by Cuttlas-U · Aug 14, 2017 at 04:55 PM
hi; if that's an array then u should do it like this :
var serializedObject = new SerializedObject(target);
var property = serializedObject.FindProperty("name of the array");
serializedObject.Update();
EditorGUILayout.PropertyField(property, true);
serializedObject.ApplyModifiedProperties();
this will find your array with the name u give to it and create it in the inspector and u don't need to do any "objectfiled" or else
The problem is that it isn't a custom editor, it's a window, so there is no "target".