[Bug][Unity 2021.1.3f1][BD 1.6.8] External behavior on prefab- variables reset in play mode

szczepan

New member
On my prefab I have a Behavior Tree component referencing external behavior1623053480005.png
I can set behavior variables on the prefab and they get serialized correctly, but as soon as I enter play mode they are reverted to defaults from external behavior. When I exit play mode, they're correct again. Note this only happens on a prefab file, not on a prefab instances created in a scene in design mode- I just can't instantiate prefabs correctly in runtime.

In general I wanted to have one external tree referenced in multiple prefab, where I would customize variables. Then I would dynamically instantiate prefab in runtime.
 
Can you reproduce it from the attached package? There are three strings set:

"External" on the external behavior tree
"Prefab" on the original prefab
"Instance" on the prefab instance in the scene

When I hit play the instance outputs "Instance", and after spawning the prefab that tree outputs "Prefab".
 

Attachments

  • repro.unitypackage
    1.8 KB · Views: 1
Thanks, I was able to narrow down the problem. Indeed when prefab is instantiated everything works fine. My mistake was that instead of actually instantiating a prefab I just created a blank component (gameObject.AddComponent<BehaviorTree>()) and copied all fields from referenced prefab. I'm able to resolve my problem by instantiating prefab and copying everything from that instance into my blank component and (since I don't need it) destroy it afterwards. Seems like an unnecessary overhead though.
You should be able to easily reproduce the problem in unity editor by highlighting prefab in project tree so that it appears in inspector and clicking play - as soon as the editor enters play mode, prefab variables are set as in the external behavior
 
Instead of using AddComponent and copying the fields I recommend just instantiating the a prefab with the external tree similar to my repro package. This will give you the cleanest workflow. When you have the prefab selected in the editor and hit play it will consume the prefab variables.
 
I had something like that earlier, but I have a large number of components on my gameobject which were getting really hard to manage, so I ended up assembling gameobject from multiple prefabs - this way I can limit the number of prefab variants.
 
Top