How to set all variables to null when disable the Behavior Tree from the scripts(not in the Task)

FireboltTZ

New member
I have a BehaviorTreePool system. It will disable behavior tree and collect behavior tree comp when the NPC was destroyed. However, the variables in the tree were not cleaned up. Is there an easy way to do that?
Otherwise, I'd have to write something like this: (and it didn't seem to be working)


public SharedVariable[] targets;

public override void OnBehaviorComplete()
{
//Set all target variables to null
for (int i = 0; i < targets.Length; i++)
{
if (targets == null)
continue;
var variable = targets;
if (variable is not SharedGameObject)
continue;
var go = variable.GetValue() as GameObject;
if(go == null)
continue;
var npc = go.GetComponent<NpcController>();
if (npc != null)
{
targets.SetValue(null);
}
var player = go.GetComponent<ThirdPersonController>();
if (player != null)
{
targets.SetValue(null);
}
}
}
 
Top