- Home /
custom inspector SerializedProperty Class List within a different Class List
I am making a custom inspector for a big script I'm working on. I have a List of a Class TestClass and within that class is another class List of LowerTestClass. Everything works fine until the code tries to run the method LowerShow and I get the error message:
NullReferenceException: Object reference not set to an instance of an object UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/EditorGUI.cs:5173) UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUILayoutOption[] options) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/EditorGUI.cs:5159) scriptToEditTest.LowerShow (UnityEditor.SerializedProperty list, System.Collections.Generic.List`1 testclassfunction) (at Assets/Editor/scriptToEditTest.cs:95) scriptToEditTest.Show (UnityEditor.SerializedProperty list, System.Collections.Generic.List`1 testclassfunction) (at Assets/Editor/scriptToEditTest.cs:87) scriptToEditTest.OnInspectorGUI () (at Assets/Editor/scriptToEditTest.cs:41) UnityEditor.InspectorWindow.DrawEditors (Boolean isRepaintEvent, UnityEditor.Editor[] editors, Boolean eyeDropperDirty) (at C:/BuildAgent/work/d3d49558e4d408f4/Editor/Mono/Inspector/InspectorWindow.cs:850) UnityEditor.DockArea:OnGUI()
Script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class scriptToBeTestedByEditor : MonoBehaviour {
public List <TestClass> testClass;
public int upperTestInt = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
[System.Serializable]
public class TestClass
{
public string className = "";
public Texture2D image;
public int testInt = 0;
public bool showLowerClass = false;
public List <LowerTestClass> lowerTestClass;
}
public class LowerTestClass
{
public string lowerClassName = "";
public Texture2D image;
public int testInt = 0;
}
Editor
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
/*[Flags]
public enum displayFieldType {
DisplayAsAutomaticFields,
DisplayAsCustomizableGUIFields
}*/
[CustomEditor(typeof(scriptToBeTestedByEditor))]
public class scriptToEditTest : Editor {
//enum displayFieldType {DisplayAsAutomaticFields, DisplayAsCustomizableGUIFields}
//displayFieldType DisplayFieldType;
int inspectorIndex = 0;
String[] inspectorOptions = {"DisplayAsAutomaticFields", "DisplayAsCustomizableGUIFields"};
/*SerializedObject GetTarget;
SerializedProperty ThisList;
int ListSize ;
void OnEnable(){
GetTarget = new SerializedObject(target);
ThisList = GetTarget.FindProperty("testClass"); // Find the List in our script and create a refrence of it
}*/
//public override
public override void OnInspectorGUI()
{
serializedObject.Update();
scriptToBeTestedByEditor testThing = (scriptToBeTestedByEditor)target;
//DisplayFieldType = EditorGUILayout.EnumPopup("Names of people:", displayFieldType);
inspectorIndex = EditorGUILayout.Popup(inspectorIndex, inspectorOptions);
if(inspectorIndex == 1)
{
testThing.testClass = Show (serializedObject.FindProperty("testClass"),testThing.testClass);
}
else if(inspectorIndex == 0)
{
base.OnInspectorGUI();
}
if(GUI.changed)
{
EditorUtility.SetDirty(testThing);
}
}
public static List <TestClass> Show (SerializedProperty list, List <TestClass> testclassfunction) {
EditorGUILayout.PropertyField(list);
EditorGUI.indentLevel += 1;
if (list.isExpanded) {
//testThing.testClass.Count
//EditorGUILayout.PropertyField(serializedObject.FindProperty("testClass").FindPropertyRelative("List.Count"));
int listSize = EditorGUILayout.IntField("list size",testclassfunction.Count);
if(listSize != testclassfunction.Count && listSize != 0){
if(listSize > testclassfunction.Count)
{
//make bigger
int numberOfForLoop = listSize-testclassfunction.Count;
for(int i = 0; i < numberOfForLoop; i++)
{
testclassfunction.Add(new TestClass{});
}
}
else if (listSize < testclassfunction.Count)
{
//make smaller
int numberOfForLoop = testclassfunction.Count - listSize;
for(int i = 0; i < numberOfForLoop; i++)
{
testclassfunction.RemoveAt(testclassfunction.Count-1);
}
}
}
EditorGUI.indentLevel += 1;
for (int i = 0; i < testclassfunction.Count; i++) {
testclassfunction[i].className = EditorGUILayout.TextField("className",testclassfunction[i].className );
testclassfunction[i].testInt = EditorGUILayout.IntField("this is int of element: " + i.ToString(),testclassfunction[i].testInt);
testclassfunction[i].showLowerClass = EditorGUILayout.Toggle("Show Lower Class?",testclassfunction[i].showLowerClass );
if(testclassfunction[i].showLowerClass)
{
testclassfunction[i].lowerTestClass = LowerShow(list.FindPropertyRelative("lowerTestClass"), testclassfunction[i].lowerTestClass);
}
}
EditorGUI.indentLevel -= 1;
}
EditorGUI.indentLevel -= 1;
return testclassfunction;
}public static List <LowerTestClass> LowerShow (SerializedProperty list, List <LowerTestClass> testclassfunction) {
EditorGUILayout.PropertyField(list);
EditorGUI.indentLevel += 1;
if (list.isExpanded) {
//testThing.testClass.Count
//EditorGUILayout.PropertyField(serializedObject.FindProperty("testClass").FindPropertyRelative("List.Count"));
int listSize = EditorGUILayout.IntField("list size",testclassfunction.Count);
if(listSize != testclassfunction.Count && listSize != 0){
if(listSize > testclassfunction.Count)
{
//make bigger
int numberOfForLoop = listSize-testclassfunction.Count;
for(int i = 0; i < numberOfForLoop; i++)
{
testclassfunction.Add(new LowerTestClass{});
}
}
else if (listSize < testclassfunction.Count)
{
//make smaller
int numberOfForLoop = testclassfunction.Count - listSize;
for(int i = 0; i < numberOfForLoop; i++)
{
testclassfunction.RemoveAt(testclassfunction.Count-1);
}
}
}
EditorGUI.indentLevel += 1;
for (int i = 0; i < testclassfunction.Count; i++) {
testclassfunction[i].lowerClassName = EditorGUILayout.TextField("lowerTestClass",testclassfunction[i].lowerClassName );
testclassfunction[i].testInt = EditorGUILayout.IntField("this is int of element: " + i.ToString(),testclassfunction[i].testInt);
}
EditorGUI.indentLevel -= 1;
}
EditorGUI.indentLevel -= 1;
return testclassfunction;
}
}
Your answer

Follow this Question
Related Questions
Custom Inspector for an array of a serialized class 0 Answers
How to create a new SerializedProperty? 0 Answers
Custom editor struggle - "Invalid iteration - (You need to stop calling Next when it returns false)" 0 Answers
[JS] [Editor] SerializedObject from a custom class. 0 Answers
Is it possible to display a List of GameObject List in a Custom Inspector? 1 Answer