- Home /
[CustomInspector] add scriptableobject in a list always null
I have a class A with a list of ScriptableObject's child class that we will call SoChild. I am writing a custom inspector for the class A.
The function OnInspectorGUI() :
// update from object
serializedObject.Update();
base.OnInspectorGUI();
//Generation
m_bFoldout= EditorGUILayout.Foldout(m_bFoldout, "List");
if(m_bFoldout)
{
SerializedProperty modules = serializedObject.FindProperty("myList");
for(int iIndex = 0; iIndex < modules.arraySize; ++iIndex)
{
SerializedProperty element = modules.GetArrayElementAtIndex(iIndex);
EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
EditorGUILayout.PropertyField(element);
if(GUILayout.Button("-"))
{
// remove one element
}
EditorGUILayout.EndHorizontal();
}
if(GUILayout.Button("Add"))
{
int iIndex = modules.arraySize;
modules.InsertArrayElementAtIndex(iIndex);
SerializedProperty newElement = modules.GetArrayElementAtIndex(iIndex);
SoChild newSoChild = ScriptableObject.CreateInstance<SoChild>();
newElement.objectReferenceValue = newSoChild ;
newElement.objectReferenceInstanceIDValue = newSoChild .GetInstanceID();
}
}
// apply modifications to object
serializedObject.ApplyModifiedProperties();
When i click on the button "Add", a new element is added to the list but the content of the property field is "None" (null). I don't understand why.
Answer by PAEvenson · Feb 04, 2013 at 06:21 PM
You use ScriptableObject.CreateInstance for when you want to create an instance of the scriptableObject at runtime. To create a new ScriptableObject in the editor you need to create the asset and add it to the project using AssetDatabase.CreateAsset.
This could help you: http://wiki.unity3d.com/index.php?title=CreateScriptableObjectAsset
Thank you for your reply.
I'd like to create instances that are children of the serialiazedObject.target. Is there any way to do that, even if the children are not ScriptableObject? Children can be different types with a common interface.
I am a little confused on what exactly you are trying to accomplish. I think of scriptableObject as a data file you reference at runtime, like storing the stats of an enemy. It lives in the project, not the scene, so if I access that scriptableObject in my level1 scene and level5 scene, that data will be the same. If I wanted to have a stronger version of that enemy for level 5 I would duplicate the scriptableObject, rename it to something like "StrongEnemy" and tweak the stats to my liking. Now it sounds like you are trying to create children of a reference in the scene. Is this what you are trying to accomplish?
The short answer is: no!
Unity doesn't support inheritance for custom classes which are marked as serialized and not derived from $$anonymous$$onoBehaviour or ScriptableObject. The alternative is to either use classes that are derived from $$anonymous$$onoBehaviour or ScriptableObject. $$anonymous$$onoBehaviours of course need to be attached to a gameobject and are serialized on their own along with the scene or a prefab. ScriptableObjects aren't serialized on their own since they don't belong to anything. You have to save them manually as assets in the editor, there's no way around that.
Is there anyway to manually serialize custom classes using inheritance? Is there some unity package to deal with it?
You can now manually serialize: http://blogs.unity3d.com/2014/06/24/serialization-in-unity/
Your answer
Follow this Question
Related Questions
Difference between assigning a value in inspector and with a custom editor script 1 Answer
A node in a childnode? 1 Answer
Loop through a List to check whether a bool is true within a ScriptableObject 1 Answer
Unity inspector field overlap with eachother 1 Answer
Making enum visible in the Inspector via a drop down list 2 Answers