- Home /
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;
}
}
}
Answer by vexe · Apr 21, 2014 at 08:35 AM
Hello :) - A couple of things:
UnityEngine.Object
s are serialized by Unity by default so you don't need to mock yourMonoBehaviour
s withSystem.Serializable
- it's redundant.List < T >
where T is a Serializable type, doesn't need to be mocked (decorated, tagged, etc) withSerializeField
(Read this article to see what are those serializable types, and what conditions have to be met for them to serialize properly)You don't need to create a new
SerializedObject
for yourtarget
, you already have aserializedObject
property that you get out of the box when you inheritEditor
- so you could use that instead.The
NullReferenceException
issue you're having, is due to the fact that you can't new up/instantiateMonoBehaviours
using thenew
operator - you can only useAddComponent
to add them (See)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 aSerializedObject
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 referencingUnityEngine.Objects
, then the first call to DeleteArrayElementAtIndex will just null the element, call it again to remove it (stupid, I know...)
in regards to the number 4 point you made, in looking at the link you supplied, it seems like im having to actually create objects in the scene in unity. Ideally what im looking to do is have the SoundObj objects just be stored in data and not in the scene. is there a way I can go about doing this?
If that's the case, you should doubt your need for them to be $$anonymous$$onoBehaviours
- can't you just make them regular classes?
Your answer
