Search Issue Tracker
Won't Fix
Votes
0
Found in
2021.3.33f1
2022.3.14f1
2023.2.2f1
2023.3.0a16
Issue ID
UUM-57058
Regression
No
"IsPartOfPrefabAsset(this)" method throws “ArgumentNullException: Value cannot be null.” when reimporting a Prefab
How to reproduce:
1. Open the "OnBeforeSerializeError 2" project
2. In the Project window right-click on the “SomePrefab” Asset and select “Reimport”
3. Observe the Console
Expected result: No errors are thrown
Actual result: The “ArgumentNullException: Value cannot be null.” error is thrown
Reproduced with: 2021.3.33f1, 2022.3.14f1, 2023.2.2f1, 2023.3.0a16
Reproduced on: macOS 14.1.1 (M1) (by reporter), Windows 11
Not reproducible on: No other environment tested
Notes:
1. Only reproducible when the Prefab has a script attached that calls “PrefabUtility.IsPartOfPrefabAsset(this)”
2. The Asset is still usable even after the error
Full error thrown to the Console:
{noformat}ArgumentNullException: Value cannot be null.
Parameter name: componentOrGameObject
UnityEngine.Bindings.ThrowHelper.ThrowArgumentNullException (System.Object obj, System.String parameterName) (at <7970836a29b044e19ce104b6d8603e32>:0)
UnityEditor.PrefabUtility.IsPartOfPrefabAsset (UnityEngine.Object componentOrGameObject) (at <2e071070fa5747148fd36a82e8d878ba>:0)
SomeComponent.OnBeforeSerialize () (at Assets/SomeComponent.cs:14)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&){noformat}
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
- Video is not rendered in Video Player when Material Override is set as the Render Mode and a SpriteRenderer is used
- Scene.isSubScene returns True when a Scene that is not a SubScene is loaded in a build
- The Search window does not show objects with inherited components when filtering by type
- Scrolling occurs when using a shortcut with "Wheel Up" or "Wheel Down" on a selection command in a scrollable Hierarchy window
- Shortcut fails to trigger when using KeyCode.WheelUp with ShortcutModifiers.Shift
Resolution Note:
I'm closing this bug as "Won't fix" for a few reasons.
1. I don't think IsPartOfPrefabAsset(this) should be used in conjunction with OnBeforeSerialize().
OnBeforeSerialize() is called every frame that the Inspector window is shown. This means that on a reimport, the script is still being called every frame the inspector is rendered. I verified this by closing the Inspector window before doing a reimport and the error did not show up. This becomes an issue if at some point in the reimport process, the prefab holding the script is rebuilt and the old version of the prefab is destroyed. If that happens while the script still runs, the script's reference to "this" might on some frame be null as the callbacks reference an object that's being destroyed. Therefore, OnBeforeSerialize() might be the wrong callback to use for this case. I looked around and it seems like this might be a useful workaround to investigate: https://stackoverflow.com/questions/52070070/unity-editor-time-script-on-gameobject-added-to-scene-event.
2. There are several work arounds to avoid this error. You can close the Inspector window and the error won't happen. You could also add a null check to catch the value of null for 'this and that also stops any errors. Another workaround I found was setting the customId from a different object. Using IsPartOfPrefabAsset(otherObject) makes it so that the script never self references null even if either object is reimported.