Search Issue Tracker
Fixed in 2021.1.X
Votes
0
Found in
2018.4
2020.1.2f1
2021.1
Issue ID
1279513
Regression
No
No way to check if a SerializedObject is null when it is initialized with null
How to reproduce:
1. Open the attached project("nullRecreation.zip")
2. Select Window > recr
3. Press "Press Me" in the recr window
Expected results: Nothing happens
Actual results: ArgumentNullRefferenceException is thrown
Reproducible with: 2018.4.28f1, 2019.4.13f1, 2020.1.11f1, 2020.2.0b9, 2021.0.a3
Notes:
1. When a SerializedObject gets initialized with null in its constructor parameter, the object ignores null checks. You also can't check serializedObject.targetObject != null, because ArgumentNullRefferenceException is thrown, because you can't access those parameters of a null value.
2. If you point a serializedObject to null then it doesn't ignore null checks
Comments (1)
-
Hosnkobf
Feb 16, 2025 19:13
Bug is back in Unity 6000.0
the decompiled code for `SerializedObject.targetObject` looks like this:
```csharp
public UnityEngine.Object targetObject
{
get
{
IntPtr intPtr = BindingsMarshaller.ConvertToNative(this);
if (intPtr == (IntPtr)0)
{
ThrowHelper.ThrowNullReferenceException(this);
}return Unmarshal.UnmarshalUnityObject<UnityEngine.Object>(get_targetObject_Injected(intPtr));
}
}
```so, it throws a null-ref-exception if the internal pointer is null. But the `BindingMarshaller` is internal as well as `m_NativeObjectPtr`. I thought I found a property that is made for public access: `isValid` - but this one is internal as well. So I am forced to use try-catch here. why?
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- [Android][BiRP] Depth processing is handled incorrectly on certain Android devices when using 2 camera's
- [APV] Cancelling Display Dialog Error is thrown after Adaptive Probe Volumes tab is open in Lighting Window
- [APV] NullReferenceException is thrown when baking Adaptive Probe Volume for a Terrain with Non-GI Contributing Tree Prototypes and multiple APV objects with different LayerMasks are present on the scene
- Hands are not recognized when using Hololens 2
- "OnTriggerExit2D" is called before "OnTriggerEnter2D" when object is destroyed immediately
Resolution Note (fix version 2021.1):
Fixed in Unity 2021.2.0a3