Referencing Scene Objects
Scene objects can be assigned directly to a scene-owned Behavior Tree, but a
project asset such as a prefab, External Behavior Tree, or global-variable asset
cannot serialize a reference to an object in a particular scene.
Choose a reference strategy
- Use a direct Inspector reference when both the tree and target live in the
same scene. - Resolve by tag, name, component, or another query when a spawned prefab can
identify the target itself. - Inject the reference from the spawner or scene controller when that system
already owns the correct object.
Prefer injection over a broad scene search when more than one object could
match.
Inject a scene object at runtime
Create a compatible local variable on the tree, bind the relevant task field to
it, and set the value after the prefab is instantiated.
using BehaviorDesigner.Runtime;
using UnityEngine;
public class AgentSpawner : MonoBehaviour
{
[SerializeField] private GameObject enemy;
public void Configure(GameObject agent)
{
var behaviorTree = agent.GetComponent<BehaviorTree>();
behaviorTree.SetVariableValue("Enemy", enemy);
}
}
Enemy must already exist as a SharedGameObject variable if task fields are
bound to it. Use a SharedTransform variable when the tasks expect a Transform
instead.
Resolve inside the tree
A Find, Find With Tag, or integration-specific sensing task can write its
result into a shared variable before the first task that consumes it. Keep that
setup on a Sequence so the consumer does not run when lookup fails.
Verify prefab behavior
Remove the original scene instance, instantiate the prefab through the same
path used by the game, and inspect the variable in Play Mode. If it remains
null, check that injection happens after the Behavior Tree exists and before a
task first reads the value.
See Variables for field binding and
External Behavior Trees for reusable assets.