SolidAlloy / ClassTypeReference-for-Unity

Property drawer that allows class selection from drop-down in Unity.
Other
298 stars 39 forks source link

NullReferenceException #28

Closed arschfidel0815 closed 3 years ago

arschfidel0815 commented 3 years ago

(This is the first issue I'm reporting on github. Pls excuse if I'm doing something wrong.)

I'm getting this NullReferenceException when I use TypeReference in a script.

NullReferenceException: Object reference not set to an instance of an object
TypeReferences.Editor.Util.SerializedTypeReference.get_GuidAssignmentFailed () (at Library/PackageCache/com.solidalloy.type-references@2371ab02f1/Editor/Util/SerializedTypeReference.cs:38)
TypeReferences.Editor.Util.SerializedTypeReference.FindGuidIfAssignmentFailed () (at Library/PackageCache/com.solidalloy.type-references@2371ab02f1/Editor/Util/SerializedTypeReference.cs:75)
TypeReferences.Editor.Util.SerializedTypeReference..ctor (UnityEditor.SerializedProperty typeReferenceProperty) (at Library/PackageCache/com.solidalloy.type-references@2371ab02f1/Editor/Util/SerializedTypeReference.cs:25)
TypeReferences.Editor.Drawers.TypeReferencePropertyDrawer.DrawTypeReferenceField (UnityEngine.Rect position, UnityEditor.SerializedProperty property) (at Library/PackageCache/com.solidalloy.type-references@2371ab02f1/Editor/Drawers/TypeReferencePropertyDrawer.cs:39)
TypeReferences.Editor.Drawers.TypeReferencePropertyDrawer.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label) (at Library/PackageCache/com.solidalloy.type-references@2371ab02f1/Editor/Drawers/TypeReferencePropertyDrawer.cs:25)
UnityEditor.PropertyDrawer.OnGUISafe (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label) (at /home/bokken/buildslave/unity/build/Editor/Mono/ScriptAttributeGUI/PropertyDrawer.cs:23)
UnityEditor.PropertyHandler.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.Rect visibleArea) (at /home/bokken/buildslave/unity/build/Editor/Mono/ScriptAttributeGUI/PropertyHandler.cs:165)
UnityEditor.GenericInspector.OnOptimizedInspectorGUI (UnityEngine.Rect contentRect) (at /home/bokken/buildslave/unity/build/Editor/Mono/Inspector/GenericInspector.cs:112)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at /home/bokken/buildslave/unity/build/External/MirroredPackageSources/com.unity.ui/Editor/Inspector/InspectorElement.cs:539)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&) (at /home/bokken/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:189)

The Serialization therefore does not work. I first used TypeReference in a ScriptableObject and thought, that it might be incompatible. But the same error occured, when I tried it with a normal MonoBehaviour script.

I would love to use this package, as it's exactly what I need in my current project. Therefore I hope that there's a simple fix for this issue! Thank you!

edit: I'm on 2020.3.12f1

SolidAlloy commented 3 years ago

Hi, thanks for reporting the issue!

The package is working fine on my end with any version of Unity.

Judging by the stack trace, NullReferenceExeption occurs here: get => _guidAssignmentFailedProperty.boolValue;

The property is found the following way: _guidAssignmentFailedProperty = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference.GuidAssignmentFailed));

I use nameof(), so if there was no such field, a compile-time error would've been thrown. Looks like a very weird Unity serialization issue.

Please check the following things:

Finally, please provide an example of how you use TypeReference inside the class. I doubt you do something unusual with it, but just to be sure.

A very well-made first issue, thanks!

arschfidel0815 commented 3 years ago

Thanks for the reply.

I've found out that the reason for the exception was purely because of wrong usage. So it was my fault.

Finally, please provide an example of how you use TypeReference inside the class.

I did this and saw, that for some reason I had a [SerializeReference] in front of my TypeReference field declaration. As soon as I removed it, the error was gone and it seems to work perfectly now.

Thanks again and sorry I have troubled you! I'm closing the issue now.