Adding a Blackboard variable causes graph to error

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;

Hi,

In order to reproduce this I think we’d need to see the rest of the graph, could we have a screenshot please or an uploaded of your project? If it’s too big or you’d rather not share then we’ll have a go with this.

Thanks

Hi @davidlovegrove

I can share the rest of the graph for sure. Although based on my report I doubt its something specific to how the graph is laid out?

This is the main graph and I have the subgraph selected:

This is the subgraph. I’ve taken parts that are relevant.


Hi,

Thanks, yeah i only asked for the rest of the graph so i can build something closer to what you’re doing :).

1 Like

I tried again today and it no longer happens. Nothing has changed with my graphs since the post above, so not really sure what is going on. It’s definitely not the first (or even second) time I have seen weird behavior like this and I think it points to a deeper issue with graph corruption that can occur under certain circumstances.

I have been testing with the demo project, and I have found with the latest version of behaviors that if I make almost any change, even just adding a new value to a blackboard, it can result in errors when I try to run in the editor. Usually the “Strike Channel” fails to be initialised correctly, despite the change I made having no relation to it. Restarting the editor seems to “fix” the issue, at least until I make another change. I suspect there is a deserialisation issue going on with behaviors in general.

Hi @susie_oms , We’re hoping to have fixed this with 1.0.8, hopefully coming out today.

@ShaneeNishry that is brilliant news! Thank you!