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 pelstw · Oct 06, 2014 at 01:05 PM · c#propertydrawerpropertyfield

Custom PropertyDrawer causes NullReferenceException

I extracted part of the code I'm working on. It's is just a simple example of the issue that causes the NullReferenceExcpection.

Basically there is a custom class, which is Serializable and has a custom PropertyDrawer. It is used in a MonoBehaviour, which is just for testing it, which has a variable of that class and an array of that class. The exception is just thrown when you also write a custom Editor for that MonoBehaviour and then start to modify values in the Inspector.

The code works fine with Unity 4.3.1, but fails to work with either Unity 4.5.4 or 4.6.x. Anyone any idea, why this stopped working?

Here is the code and i also put a package on my dropbox.

MyClass.cs

 using UnityEngine;
 using System;
 
 [Serializable]
 public class MyClass
 {
     public string name;
 }


MyClassDrawer.cs

 using UnityEditor;
 using UnityEngine;
 
 [CustomPropertyDrawer( typeof( MyClass ) )]
 public class MyClassDrawer : PropertyDrawer
 {
     public override void OnGUI( Rect position, SerializedProperty property, GUIContent label )
     {
         SerializedProperty name = property.FindPropertyRelative( "name" );
         
         label = EditorGUI.BeginProperty( position, label, property );
         EditorGUI.PropertyField( position, property.FindPropertyRelative( "name" ) );
         EditorGUI.EndProperty();
     }
 }


MyClassTester.cs

 using UnityEngine;
 
 public class MyClassTester : MonoBehaviour 
 {
     public MyClass myClass;
     public MyClass[] myClassArray;
 }


MyClassTesterEditor.cs

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor( typeof( MyClassTester ) )]
 public class MyClassTesterEditor : Editor
 {
     public override void OnInspectorGUI()
     {
         serializedObject.Update();
         EditorGUILayout.PropertyField( serializedObject.FindProperty( "myClass" ) );
         EditorGUILayout.PropertyField( serializedObject.FindProperty( "myClassArray" ), true );
         serializedObject.ApplyModifiedProperties();
 
     }
 }


Here is the according stack trace:

 NullReferenceException: Object reference not set to an instance of an object
 UnityEditor.EditorGUI.BeginProperty (Rect totalPosition, UnityEngine.GUIContent label, UnityEditor.SerializedProperty property) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/EditorGUI.cs:3859)
 UnityEditor.EditorGUI.DefaultPropertyField (Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/EditorGUI.cs:4346)
 UnityEditor.PropertyHandler.OnGUI (Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/ScriptAttributeGUI/PropertyHandler.cs:144)
 UnityEditor.EditorGUI.PropertyFieldInternal (Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/EditorGUI.cs:4341)
 UnityEditor.EditorGUI.PropertyField (Rect position, UnityEditor.SerializedProperty property, Boolean includeChildren) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/EditorGUIBindings.cs:698)
 UnityEditor.EditorGUI.PropertyField (Rect position, UnityEditor.SerializedProperty property) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/EditorGUIBindings.cs:693)
 MyClassDrawer.OnGUI (Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label) (at Assets/Editor/MyClassDrawer.cs:12)
 UnityEditor.PropertyDrawer.OnGUISafe (Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/ScriptAttributeGUI/PropertyDrawer.cs:23)
 UnityEditor.PropertyHandler.OnGUI (Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/ScriptAttributeGUI/PropertyHandler.cs:134)
 UnityEditor.PropertyHandler.OnGUILayout (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/ScriptAttributeGUI/PropertyHandler.cs:195)
 UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/EditorGUI.cs:5992)
 UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/EditorGUI.cs:5986)
 MyClassTesterEditor.OnInspectorGUI () (at Assets/Editor/MyClassTesterEditor.cs:14)
 UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean forceDirty, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect, Boolean eyeDropperDirty) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/Inspector/InspectorWindow.cs:1124)
 UnityEditor.DockArea:OnGUI()


Comment
Add comment · Show 2
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
avatar image invicticide · Oct 07, 2014 at 09:09 PM 0
Share

I've just run into the exact same problem. :(

Will keep an eye on this thread and post back if I figure anything out.

avatar image pelstw · Oct 11, 2014 at 09:00 AM 0
Share

creating a custom editor that iterates over all the arrays or lists items and creates a property field for each solves the issues, but is potentially much more work to recreate all the functionality.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by frarees · Oct 11, 2014 at 02:12 PM

The problem here is that your drawer is called both for MyClass and MyClass[]. The first draw at MyClassTesterEditor.cs:11 works great, but the second one at MyClassTesterEditor.cs:12 does not, cause property now contains the actual array, so there's no name property to look for.

You can check that yourself using modifying your drawer:

 using UnityEditor;
 using UnityEngine;
 
 [CustomPropertyDrawer (typeof(MyClass))]
 public class MyClassDrawer : PropertyDrawer {
     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
         label = EditorGUI.BeginProperty (position, label, property);
         if (property.isArray)
             EditorGUI.PropertyField (position, property, false);
         else
             EditorGUI.PropertyField (position, property.FindPropertyRelative ("name"), false);
         EditorGUI.EndProperty ();
     }
 }
 

I don't actually know if this behavior has changed after 4.3.1, but maybe after implementing the Decorators on 4.5 they may have changed this API.

Comment
Add comment · Show 1 · Share
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
avatar image marconi · Oct 23, 2014 at 06:38 PM 0
Share

awesome, that was exactly it.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Custom attribute not respecting property's PropertyDrawer 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Multiple Cars not working 1 Answer

How does one access the Label argument of a PropertyField within a PropretyDrawer using UIToolkit/UIElements? 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