Reply To: Subtree Reference Lost on Prefab

Forums 💬 NodeCanvas ⚙️ Support Subtree Reference Lost on Prefab Reply To: Subtree Reference Lost on Prefab

#19548
Gavalakis
Keymaster

    Hello and thanks 🙂

    The problem you are facing is the fact that Unity does not serialize references from the scene. So (if I understoon correctly), you have your NPC B in the scene, drag the NPC A subtree from the scene and then make a prefab of NPC B. As a result, prefab NPC B is referencing a scene Object, which Unity can’t store. (Instantiating NPC A works, because the SubTree object is within the prefab of NPC A)

    What you would want to do is to either reference prefabs in the scene, or reference prefabs in prefabs.
    So here is a workflow:

    SubTrees are just another tree. You can create a standalone tree through “Window/NodeCanvas/Create Behaviour Tree” Unity top menu.

    Create NPC A (BehaviourTreeOwner).
    Add SubTree node and assign the previously created standalone subtree.
    Edit as you please.
    Make a prefab of the SubTree, and delete it from the scene.
    Now assign the new prefab SubTree to the SubTree Node.

    Make NPC B (BehaviourTreeOwner)
    Do the same as NPC A (create SubTree node, and assign the prefab SubTree)

    So in the end you will have 3 prefabs. NPC A, NPC B, and SubTree.
    Now instantiating the NPCs will keep the references to the subtree, which will be instantiated automaticaly for each NPC when nescessary in runtime.

    The worfklow will be much easier in the next version, where I will enable the ability to view/edit prefab trees, which right now being impossible is a bit of a pain when working with prefabs.

    Cheers. Let me know if this works for you 🙂