Null Reference from Custom Editor

So Ive been getting null errors on the SoundObj In the DropAreaGUI function in the SoundInspector Class. Im unsure what im doing wrong, but even if i try and play it all the data gets wiped away and im really confused what Im doing wrong. please help.

//SoundManager

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class SoundManager : MonoBehaviour {
    
    [SerializeField]
    public List<SoundObj> list;

    public void Start()
    {
         list = new List<SoundObj>();
    }
}

//SoundObj

using UnityEngine;

using System.Collections;
using FMOD.Studio;

[System.Serializable]
public class SoundObj : MonoBehaviour
{
   //Nothing in here right now
}

//SoundInspector

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

[CanEditMultipleObjects]
[CustomEditor(typeof(SoundManager))]
public class SoundInspector : Editor
{

private SerializedObject obj;
public List<SoundObj> list;
public SoundManager TargetObject;
public List<bool> objFold;

public void OnEnable()
{
    obj = new SerializedObject(target);
    TargetObject = target as SoundManager;
    list = TargetObject.list;
    objFold = new List<bool>();
    if(list!=null){
        for (int n = 0; n < list.Count; n++)
            objFold.Add(true);
    }
}
private void RemovePoolAtIndex(int index)
{
    for (int n = index; n < list.Count - 1; n++)
        list[n] = list[n + 1];

    list.RemoveAt(list.Count - 1);
}
public void DropAreaGUI()
{
    Event evt = Event.current;
    Rect drop_area = GUILayoutUtility.GetRect(0.0f, 50.0f, GUILayout.ExpandWidth(true));
    GUI.Box(drop_area, "Add FMODASSET Into Box");

    switch (evt.type)
    {
        case EventType.DragUpdated:
        case EventType.DragPerform:
            if (!drop_area.Contains(evt.mousePosition))
                return;

            DragAndDrop.visualMode = DragAndDropVisualMode.Copy;

            if (evt.type == EventType.DragPerform)
            {
                DragAndDrop.AcceptDrag();

                foreach (Object dragged_object in DragAndDrop.objectReferences)
                {
                    FMODAsset asset = dragged_object as FMODAsset;
                    if (asset == null)
                    {
                        continue;
                    }
                    SoundObj obj = new SoundObj();
                    list.Add(new SoundObj());
                }
            }
            Event.current.Use();
            break;
    }
}

}

Hello :slight_smile: - A couple of things:

  1. UnityEngine.Objects are serialized by Unity by default so you don’t need to mock your MonoBehaviours with System.Serializable - it’s redundant.
  2. List < T > where T is a Serializable type, doesn’t need to be mocked (decorated, tagged, etc) with SerializeField (Read this article to see what are those serializable types, and what conditions have to be met for them to serialize properly)
  3. You don’t need to create a new SerializedObject for your target, you already have a serializedObject property that you get out of the box when you inherit Editor - so you could use that instead.
  4. The NullReferenceException issue you’re having, is due to the fact that you can’t new up/instantiate MonoBehaviours using the new operator - you can only use AddComponent to add them (See)
  5. Finally, you created a SerializedObject for your target, but you yet you’re using a direct value to your list when removing items, which makes the whole point of using a SerializedObject pointless. I assume you intended to use SerializedProperties? - Then, to remove an element for your list via the serialized property that’s pointing to it, you could just: spList.DeleteArrayElementAtIndex(index); (if your list is referencing UnityEngine.Objects, then the first call to DeleteArrayElementAtIndex will just null the element, call it again to remove it (stupid, I know…)