- Home /
OnSelectionChange not working
I'm following along with a tutorial about creating a behavior tree graph UI Builder and GraphView and I'm stuck. When the tutorial maker tries to instantiate a node, it works, but when I try, it gives me a NullReferenceException
error related to the behavior tree itself not existing. BehaviorTree
is a scriptable object that I have in the assets folder. The BehaviorTreeView
script inherits from GraphView
and holds a BehaviorTree tree
variable to populate the tree with Node
objects. The tree is set by a function PopulateView(BehaviorTree tree)
as seen below.
internal void PopulateView(BehaviorTree tree)
{
this.tree = tree;
Debug.Log(tree);
DeleteElements(graphElements);
tree.nodes.ForEach(n => CreateNodeView(n));
}
This script is supposed to be called by an EditorWindow
script, BehaviorTreeEditor
, using OnSelectionChange()
as seen below. Currently it is not called at all.
private void OnSelectionChange()
{
Debug.Log("Selection changed");
BehaviorTree tree = Selection.activeObject as BehaviorTree;
if (tree)
{
treeView.PopulateView(tree);
}
}
This function doesn't fire at all when I select an object in either the object hierarchy or in the asset window. The script is in the Editor folder under Assets. In the tutorial I'm following, selecting the BehaviorTree
asset enables the population of the UI with nodes, but for some reason when I do it it doesn't work. Am I missing something about how OnSelectionChanged works?
Your answer
![](https://koobas.hobune.stream/wayback/20220613054516im_/https://answers.unity.com/themes/thub/images/avi.jpg)