Skip to content

Commit 5479279

Browse files
committed
Treat unassigned Unity object fields as null when reading them by reflection
1 parent 24f13b0 commit 5479279

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

‎Packages/net.pawlygon.unitytools/Editor/FaceTrackingExtrasCore.cs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,24 @@ private static T GetMember<T>(object target, string name) where T : class
344344
Type type = target.GetType();
345345

346346
FieldInfo field = type.GetField(name, flags);
347-
if (field != null) return field.GetValue(target) as T;
347+
if (field != null) return AsLiveValue<T>(field.GetValue(target));
348348

349349
PropertyInfo property = type.GetProperty(name, flags);
350-
if (property != null && property.GetIndexParameters().Length == 0) return property.GetValue(target) as T;
350+
if (property != null && property.GetIndexParameters().Length == 0) return AsLiveValue<T>(property.GetValue(target));
351351

352352
return null;
353353
}
354354

355+
/// <summary>
356+
/// An unassigned or destroyed Unity object field (e.g. a PhysBone with no Root Transform) reads back as a
357+
/// non-null placeholder that throws on use; return real null so callers' fallbacks apply.
358+
/// </summary>
359+
private static T AsLiveValue<T>(object value) where T : class
360+
{
361+
if (value is UnityEngine.Object unityObject && unityObject == null) return null;
362+
return value as T;
363+
}
364+
355365
// =====================================================================
356366
// Paths
357367
// =====================================================================

0 commit comments

Comments
 (0)