Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by orangeflame · Mar 29, 2015 at 05:13 AM · classeditorguilayoutserializedpropertylist of lists

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;
     }
 
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges