I’m not entirely sure why this happens, but when I add a new variable to a Blackboard Asset that is being used inside a subgraph inside my main graph then I get an error on one of my custom nodes.
The error:
NullReferenceException: Object reference not set to an instance of an object
CanHearCondition.IsTrue () (at Assets/Scripts/Behaviour/Conditions/CanHearCondition.cs:16)
Unity.Behavior.ConditionUtils.CheckConditions (System.Collections.Generic.List`1[T] conditions, System.Boolean allRequired) (at ./Library/PackageCache/com.unity.behavior/Runtime/Execution/Conditions/ConditionUtils.cs:117)
Unity.Behavior.ConditionalGuardAction.OnStart () (at ./Library/PackageCache/com.unity.behavior/Runtime/Execution/Nodes/Actions/Conditional/ConditionalGuardAction.cs:35)
Unity.Behavior.Node.Start () (at ./Library/PackageCache/com.unity.behavior/Runtime/Execution/Node.cs:103)
Unity.Behavior.BehaviorGraphModule.StartNode (Unity.Behavior.Node node) (at ./Library/PackageCache/com.unity.behavior/Runtime/Execution/BehaviorGraphModule.cs:117)
Unity.Behavior.Node.StartNode (Unity.Behavior.Node node) (at ./Library/PackageCache/com.unity.behavior/Runtime/Execution/Node.cs:176)
Unity.Behavior.SequenceComposite.OnUpdate () (at ./Library/PackageCache/com.unity.behavior/Runtime/Execution/Nodes/Composites/SequenceComposite.cs:48)
Unity.Behavior.Node.Update () (at ./Library/PackageCache/com.unity.behavior/Runtime/Execution/Node.cs:112)
Unity.Behavior.BehaviorGraphModule.Tick () (at ./Library/PackageCache/com.unity.behavior/Runtime/Execution/BehaviorGraphModule.cs:85)
Unity.Behavior.RunSubgraph.OnUpdate () (at ./Library/PackageCache/com.unity.behavior/Runtime/Execution/Nodes/Actions/RunSubgraph.cs:34)
Unity.Behavior.Node.Update () (at ./Library/PackageCache/com.unity.behavior/Runtime/Execution/Node.cs:112)
Unity.Behavior.BehaviorGraphModule.Tick () (at ./Library/PackageCache/com.unity.behavior/Runtime/Execution/BehaviorGraphModule.cs:85)
Unity.Behavior.BehaviorGraph.Tick () (at ./Library/PackageCache/com.unity.behavior/Runtime/Execution/BehaviorGraph.cs:60)
Unity.Behavior.BehaviorGraphAgent.Update () (at ./Library/PackageCache/com.unity.behavior/Runtime/Execution/Components/BehaviorGraphAgent.cs:515)
My graph uses a Blackboard asset called Creature. Here is what it looks like before I add any new variable and everything works fine (no errors):
This is what it looks like after I add a new variable at the bottom:
Then when running the graph at runtime, the error pasted above starts to show. This is the code from my custom node that is used inside of a subgraph inside the main graph. The error points to the line 16 below.
using System;
using Unity.Behavior;
using UnityEngine;
[Serializable, Unity.Properties.GeneratePropertyBag]
[Condition(name: "Can Hear", story: "[Agent] can hear [Transform] within [Distance] distance", category: "Conditions", id: "86d99f9673b1b0f182aae851ed6f25c7")]
public partial class CanHearCondition : Condition {
[SerializeReference] public BlackboardVariable<Transform> Agent;
[SerializeReference] public BlackboardVariable<Transform> Transform;
[SerializeReference] public BlackboardVariable<float> Distance;
RaycastHit[] m_HitsCache = new RaycastHit[32];
float m_OccludedMultiplier = 0.75f;
public override bool IsTrue() {
float distance = Vector3.Distance(Agent.Value.position, Transform.Value.position);
// Transform is too far away to hear anything anyway
if (distance >= Distance) return false;
// Cast to see if the Player can be seen
var hitCount = Physics.RaycastNonAlloc(Agent.Value.position, Transform.Value.position - Agent.Value.position, m_HitsCache, float.PositiveInfinity, ObjectLayerMask.SolidObjects, QueryTriggerInteraction.Ignore);
// Sort by closest first
PhysicsUtility.Sort(m_HitsCache, hitCount);
bool hasPassedThrough = false;
for (int i = 0; i < hitCount; i++) {
var hitInfo = m_HitsCache[i];
// Check if the hit is closer than the audio source.
if (hitInfo.distance < distance) {
var surfaceType = hitInfo.GetSurfaceType(true);
// Check reduced distance if surface is not a pass through, or we have already passed through one surface.
if (surfaceType == ObjectSurfaceType.None || hasPassedThrough) {
return hitInfo.distance >= Distance * m_OccludedMultiplier;
}
hasPassedThrough = true;
}
}
// Clear cache to avoid keeping references from being GCed.
Array.Clear(m_HitsCache, 0, hitCount);
return true;
}
}
Literally the only thing that changes is that I add a variable to the Creature blackboard asset. If I undo my changes, the error goes away. So I guess a link is getting corrupted somewhere when adding new variables? I inspected the Subgraph node and the specific node inside the subgraph and they look fine both before/after adding the new variable.
In case it makes a difference, this graph gets initialized via code.
BehaviorGraphAgent.Graph = CreatureDefinition.BehaviorGraph;
BehaviorGraphAgent.Init();
BehaviorGraphAgent.enabled = true;