Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by Yanfundi · May 08 at 08:06 PM · custom editorproperty drawer

Property Drawer SerializedProperty Null inside Array


I'm new to this PropertyDrawer stuff. I have created PropertyDrawer for my class AudioData and it works fine. But when I create variable that is an array/list of AudioData and click the plus button, it throws a NullException to my property.FindPropertyRelative.


How do I fix this? Thanks.


 using UnityEngine;
 
 #if UNITY_EDITOR
 using UnityEditor;
 
 [CustomPropertyDrawer(typeof(AudioData), false)]
 public class AudioDataPropertyDrawer : PropertyDrawer
 {
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return property.isExpanded ? (EditorGUIUtility.singleLineHeight * 5) : EditorGUIUtility.singleLineHeight;
     }
 
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUI.BeginProperty(position, label, property);
         if (property != null)
         {
             string foldOutName = string.IsNullOrEmpty(property.FindPropertyRelative("AudioName").stringValue) ? "Audio Data" : property.FindPropertyRelative("AudioName").stringValue;
             Rect foldOutRect = new Rect(position.x, position.y, position.width, 16);
             property.isExpanded = EditorGUI.Foldout(foldOutRect, property.isExpanded, foldOutName, true);
             EditorGUI.indentLevel++;
 
             if (property.isExpanded)
             {
                 Rect nameRect = new Rect(position.x, position.y + 18, position.width, 16);
                 EditorGUI.PropertyField(nameRect, property.FindPropertyRelative("AudioName"));
 
                 Rect clipRect = new Rect(position.x, position.y + 36, position.width, 16);
                 EditorGUI.PropertyField(clipRect, property.FindPropertyRelative("SoundClip"));
 
                 Rect isRandVolRect = new Rect(position.x, position.y + 54, position.width, 16);
                 EditorGUI.PropertyField(isRandVolRect, property.FindPropertyRelative("IsRandomizeVolume"));
 
                 Rect volumeRect = new Rect(position.x, position.y + 72, position.width, 16);
                 if (property.FindPropertyRelative("IsRandomizeVolume").boolValue)
                     EditorGUI.PropertyField(volumeRect, property.FindPropertyRelative("VolumeRange"));
                 else
                 {
                     Vector2 volX = property.FindPropertyRelative("VolumeRange").vector2Value;
                     volX.x = EditorGUI.Slider(volumeRect, "Volume", property.FindPropertyRelative("VolumeRange").vector2Value.x, 0, 1);
                     property.FindPropertyRelative("VolumeRange").vector2Value = volX;
                 }
             }
             EditorGUI.indentLevel--;
         }
         EditorGUI.EndProperty();
     }
 }
 #endif
 
 public enum SoundTypeEnum
 {
     SFX,
     BGM,
 }
 
 [System.Serializable]
 public class AudioData
 {
     public string AudioName = "";
     public AudioClip SoundClip = null;
     public bool IsRandomizeVolume = false;
     public Vector2 VolumeRange = Vector2.one;
     
     public virtual AudioClip GetSoundClip(object[] obj = null)
     {
         return SoundClip;
     }
     public virtual float GetVolume(object[] obj = null)
     {
         return IsRandomizeVolume ? Random.Range(VolumeRange.x, VolumeRange.y) : VolumeRange.x;
     }
 
     public AudioData()
     {
         AudioName = "";
         SoundClip = null;
         IsRandomizeVolume = false;
         VolumeRange = Vector2.one;
     }
 
     public AudioData(string name = "", AudioClip clip = null, bool isRandomizeVol = false, Vector2? vol = null)
     {
         AudioName = name;
         SoundClip = clip;
         IsRandomizeVolume = isRandomizeVol;
         VolumeRange = vol ?? Vector2.one;
     }
     public AudioData(AudioClip clip = null, bool isRandomizeVol = false, Vector2? vol = null)
     {
         AudioName = clip.name;
         SoundClip = clip;
         IsRandomizeVolume = isRandomizeVol;
         VolumeRange = vol ?? Vector2.one;
     }
 }


This is what happened in the Editor (Attached GIF):


alt text


The error:

 NullReferenceException: Object reference not set to an instance of an object
 AudioDataPropertyDrawer.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label) (at Assets/_Project/Scripts/Managers/Audio Manager/AudioData.cs:19)
 UnityEditor.PropertyDrawer.OnGUISafe (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditor.PropertyHandler.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.Rect visibleArea) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditor.PropertyHandler.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditorInternal.ReorderableList+Defaults.DrawElement (UnityEngine.Rect rect, UnityEditor.SerializedProperty element, System.Object listItem, System.Boolean selected, System.Boolean focused, System.Boolean draggable, System.Boolean editable) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditorInternal.ReorderableList.DoListElements (UnityEngine.Rect listRect, UnityEngine.Rect visibleRect) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditorInternal.ReorderableList.DoList (UnityEngine.Rect rect, UnityEngine.Rect visibleRect) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditorInternal.ReorderableListWrapper.Draw (UnityEngine.GUIContent label, UnityEngine.Rect r, UnityEngine.Rect visibleArea, System.Boolean includeChildren) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditor.PropertyHandler.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.Rect visibleArea) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditor.PropertyHandler.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditor.PropertyHandler.OnGUILayout (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, System.Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditor.Editor.DoDrawDefaultInspector (UnityEditor.SerializedObject obj) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditor.Editor.DoDrawDefaultInspector () (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditor.Editor.DrawDefaultInspector () (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditor.Editor.OnInspectorGUI () (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
 UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <17cba49fae414354b79f447a7073fc5b>:0)
 UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)


audiodata-propertydrawer.gif (64.9 kB)
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

138 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 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 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 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 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 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 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

Property Drawer ArgumentException 1 Answer

Property drawer with multiple fields: only first field has prefab override implemented 1 Answer

OnSceneGUI with PropertyDrawer 2 Answers

Use placeholder text in CustomEditor 0 Answers

Draw rect color disappearing when any input is detected 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