EDIT: The solution is to perform the following additional check:
csharp** ** if (kvp.Value.unboxed is UnityEngine.Object o) { if (o == null) continue; }** **
The following code is crashing with a null reference exception:
foreach (KeyValuePair<string, ContextBox> kvp in all)
{
if (kvp.Value.unboxed == null) continue;
CareerData.SaveString("CTX_" + kvp.Key, kvp.Value.unboxed.UID());
}
Single-stepping through, the exception arises inside UID() because the monobehaviour whose interface I am holding in the âunboxedâ variable has been destroyed.
VisualStudio displays the value of unboxed as ânullâ, not null. Presumably, the fact Iâm holding an interface reference rather than a monobehaviour reference is bypassing the usual safeguards and sneaking past my null check.
Not all IContext interfaces belong to monobehaviours. Is there a safe test I can make to fix this, before I throw my hands up and wrap the whole thing in try/catch?