diff --git a/JotunnModStub/Factions/FactionHostilityPatch.cs b/JotunnModStub/Factions/FactionHostilityPatch.cs index b445dbe..7fee617 100644 --- a/JotunnModStub/Factions/FactionHostilityPatch.cs +++ b/JotunnModStub/Factions/FactionHostilityPatch.cs @@ -207,14 +207,23 @@ namespace RacesOfValheim { if (!FactionsConfig.Hostility) return; Traverse targetStatic = Traverse.Create(__instance).Field("m_targetStatic"); - if (targetStatic.GetValue() == null) return; + // GetValue() hands back a plain object, and object == null skips Unity's destroyed + // check: a piece smashed or removed while targeted stays non-null here, and touching + // its gameObject throws. That throw escapes into MonoUpdaters.FixedUpdate and stops + // every AI after this one for the frame, every frame, until the field changes. + Component target = targetStatic.GetValue() as Component; + if (target == null) + { + if (targetStatic.GetValue() != null) targetStatic.SetValue(null); + return; + } Character monster = Traverse.Create(__instance).Field("m_character").Value; if (monster == null) return; // A piece an allied race built is never a target, with or without anyone around. // See FactionPieceTruce. - if (targetStatic.GetValue() is Component target && FactionPieceTruce.Protects(target.gameObject, monster)) + if (FactionPieceTruce.Protects(target.gameObject, monster)) { targetStatic.SetValue(null); return;