Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 kroltan_ · Aug 27, 2015 at 12:28 PM · c#editor-scriptingserializationeditorwindowserializedobject

SerializedProperty and PropertyField NullReference

I'm having a strange problem in using EditorGUILayout.PropertyField on my EditorWindow:

There is a master object that holds a reference to lists of Items, each item containing a preview image. These 3 classes (Master, Item, Preview) are custom ScriptableObjects.

In my editor window, I construct a SerializedObject of the Master, get the item list property, and extract the item's actual value (the objectReferenceValue). This item is stored on the editor class, and later a GUI is displayed to edit the Preview's properties.

Such GUI again creates a SerializedObject out of the Item, gets its Preview property, and using FindPropertyRelative creates PropertyFields for each of the Preview's properties.

The GUI drawing function (called inside the window's OnGUI):

 private void DrawItemDetail() {
     if (_currentItem == null) {
         return;
     }
     var currentItemSerial = new SerializedObject(_currentItem);
     var previewSerial = new SerializedObject(_currentItem.PreviewImage);
 
     GUILayout.BeginVertical();
     GUILayout.Label("Preview Image Settings", EditorStyles.boldLabel);
     EditorGUI.indentLevel++;
     EditorGUILayout.PropertyField(previewSerial.FindProperty("DistanceToPivot"), new GUIContent("Distance to Pivot"));
     EditorGUILayout.PropertyField(previewSerial.FindProperty("PivotPosition"), new GUIContent("Pivot Position"));
     EditorGUILayout.PropertyField(previewSerial.FindProperty("PivotRotation"), new GUIContent("Pivot Rotation"));
     EditorGUI.indentLevel--;
     GUILayout.EndVertical();
 }

Note the 3 PropertyField calls. Whichever comes first, gives the following stack trace:

 NullReferenceException: Object reference not set to an instance of an object
 UnityEditor.PropertyHandler.OnGUILayout (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Editor/Mono/ScriptAttributeGUI/PropertyHandler.cs:190)
 UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:6837)
 UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:6825)
 EditMode.Editor.CatalogWindow.DrawItemDetail () (at Assets/Exosphir/Scripts/EditMode/Editor/CatalogWindow.cs:220)
 EditMode.Editor.CatalogWindow.OnGUI () (at Assets/Exosphir/Scripts/EditMode/Editor/CatalogWindow.cs:89)
 System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)

It's worth noting that the 3 properties being accessed are a float, a Vector3 and a Quaternion. I have tried getting the preview property from the serialized object, then using FindPropertyRelative to get the properties, but a similar error happens. How can I fix this?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by kroltan_ · Aug 27, 2015 at 06:06 PM

Found it, the problem was that I was actually using C# properties ( {get;set;}) for the PreviewImage properties, and Unity doesn't serialize that. Changing them to fields fixed it.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Custom Editor window loses values on play. 1 Answer

Filling list with editor scripts is empty on Play 1 Answer

Show camera view in an editor window 1 Answer

Code executes more than once when key press event is triggered in OnGui in Custom Editor Window 3 Answers

Add lines to a script from another script 0 Answers


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