Skip to main content
Named includeChildren to make it explicit why that "true" is necessary.
Source Link
Tim C
  • 645
  • 3
  • 11

You can't do this with ObjectField because Arrays are telling the editor that it is of type Tile in MyScriptEditornot Objects. Instead, castYou need to use a Tile arrayPropertyField instead of an ObjectField, which is more generic and use typeof for the arraycan handle properties of any type.

typeof(Tile[]) 

Corrected code:

[CustomEditor(typeof(TileMyTile))] public class MyScriptEditor : Editor   { override public void OnInspectorGUI()  { var tile = target as Tile;MyTile; tile.variety = EditorGUILayout.Toggle("Hide Fields", tile.variety); if (tile.variety)  { tile.varietyTilesSerializedProperty tileProperty = serializedObject.FindProperty(Tile[]"varietyTiles"); if(EditorGUILayout.ObjectFieldPropertyField("Tile", tiletileProperty, typeof(Tile[]includeChildren:true),)  allowSceneObjects: true { serializedObject.ApplyModifiedProperties(); } } } } 

You are telling the editor that it is of type Tile in MyScriptEditor. Instead, cast to a Tile array and use typeof for the array type.

typeof(Tile[]) 

Corrected code:

[CustomEditor(typeof(Tile))] public class MyScriptEditor : Editor { override public void OnInspectorGUI() { var tile = target as Tile; tile.variety = EditorGUILayout.Toggle("Hide Fields", tile.variety); if (tile.variety) { tile.varietyTiles = (Tile[])EditorGUILayout.ObjectField("Tile", tile, typeof(Tile[]), allowSceneObjects: true); } } } 

You can't do this with ObjectField because Arrays are not Objects. You need to use a PropertyField instead of an ObjectField, which is more generic and can handle properties of any type.

Corrected code:

[CustomEditor(typeof(MyTile))] public class MyScriptEditor : Editor  { override public void OnInspectorGUI()  { var tile = target as MyTile; tile.variety = EditorGUILayout.Toggle("Hide Fields", tile.variety); if (tile.variety)  { SerializedProperty tileProperty = serializedObject.FindProperty("varietyTiles"); if(EditorGUILayout.PropertyField(tileProperty, includeChildren:true))   { serializedObject.ApplyModifiedProperties(); } } } } 
Source Link
Tim C
  • 645
  • 3
  • 11

You are telling the editor that it is of type Tile in MyScriptEditor. Instead, cast to a Tile array and use typeof for the array type.

typeof(Tile[]) 

Corrected code:

[CustomEditor(typeof(Tile))] public class MyScriptEditor : Editor { override public void OnInspectorGUI() { var tile = target as Tile; tile.variety = EditorGUILayout.Toggle("Hide Fields", tile.variety); if (tile.variety) { tile.varietyTiles = (Tile[])EditorGUILayout.ObjectField("Tile", tile, typeof(Tile[]), allowSceneObjects: true); } } }