'Unity - Custom Editor of List don't show properties but a gameobject

I went into an issue while creating a custom editor and using a List of a custom class.

I had the following script for the classes:

customClass.cs

[System.Serializable]
public class customClass : MonoBehaviour
{
    public int test;
}

classToEdit.cs

public class classToEdit : MonoBehaviour
{
    [SerializeField]
    private List<customClass> customClassList;
}

I had this script for the editor class:

classToEditEditor.cs

[CustomEditor(typeof(classToEdit))]
public class classToEditEditor : Editor
{
    public override void OnInspectorGUI()
    {
        // Update the serialize object
        serializedObject.Update();

        // Display properties
        EditorGUILayout.PropertyField(serializedObject.FindProperty("customClassList"), true);

        // Apply modif
        serializedObject.ApplyModifiedProperties();
    }
}

And when I go to the inspector, I have the following: enter image description here

Fact is I don't want to drag and drop something, I want to set my complete object here.

Below is the solution for the future me and you !

Have a good day !



Solution 1:[1]

The solution is quite simple: Just remove the MonoBehaviour class from whiwh derive your custom object. This is automatically added by Unity but remove it.

So, you customClass.cs is now like this:

[System.Serializable]
public class customClass
{
    public int test;
}

By doing that, you'll have the following: enter image description here

Few tips when creating a custom editor:

  • Don't forget to remove the MonoBehaviour class when using a List of your custom class
  • Don't forget to add System.Serializable before your custom class
  • In your editor, don't forget to set the includeChildren at true in the EditorGUILayout.PropertyField method

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adrien G.