How to Set Unique Behavior Graph Variables for Each BehaviorAgent?

Hello everyone,

I’m having an issue with Behavior Graph in Unity and would really appreciate some guidance.

The Problem:

I have a BehaviorGraph that I use for multiple BehaviorAgents. However, when I set a variable using the following methods in code:

behaviorAgent.SetVariableValue("variableName", value);
behaviorAgent.BlackboardReference.SetVariableValue("variableName", value);

Both approaches result in the variable being changed globally, meaning the new value applies to all agents instead of just the specific agent I intended to modify. This breaks the independence of each agent in the game.

Interesting Observation:

  • If I manually edit the variable in the Inspector for each agent, it works correctly—each agent retains its own unique value.
  • I want to achieve this behavior through code, but I’m not sure what I’m doing wrong.

My Questions:

  • Am I missing a step when setting variables in the Behavior Graph?
  • How can I ensure that each agent has its own unique set of variables?

Any help would be greatly appreciated! Thanks in advance!

1 Like

Hi kietgula,

Are you trying to edit the same variable when you do it manually? I’m wondering if there is any chance that the variable you tried to modify from code was marked as ‘Shared’ in the blackboard, e.g.:

If not, I’m not sure - the code you use looks like it should work to me. Kind regards,

Andy

1 Like

I was also going to ask the same question as Andy :slight_smile: Thanks for the support Andy!
It should be set on an individual basis unless the variable is marked as Shared.

1 Like

Thank for your answer.

But that’s not the case. Even with ‘Shared’ turned off, the issue still occurs.

However, after following ChatGPT’s suggestion and instantiating the Behavior Graph, it finally worked—though in a somewhat unexpected way.

 var aiAgent = unitGo.GetComponent<BehaviorGraphAgent>();
        aiAgent.Graph = Instantiate(behaviorGraph);
        aiAgent.SetVariableValue("AttackRange", unitData.stats.attackRange);
        aiAgent.SetVariableValue("TargetTag", TargetTag);

Hi @kietgula

The issue you are describing has been identified a few week ago. It as to do with the fact that before an agent is initialized (Init), it’s blackboard variables are referencing the original asset. I believe that calling aiAgent.Init before assigning the variable might fix your issue.

I cannot comment on when a fix will be released atm.
Sorry for the inconvience :bowing_man:

1 Like

To ensure each agent has its own unique set of variables, you need to create a separate instance of the BehaviorGraph for each agent instead of reusing a single graph.

No, variables are not shared between agents unless they are marked as shared, but we established earlier in the thread that that was not the issue here. I’m guessing the issue & workaround given by @MorganHoarauUnity above is sufficient to explain/fix it, though we haven’t heard back from the OP whether that worked.

1 Like