Game crashes if custom Pawn converted to BP

Hi!

I have an issue with converting my custom Pawn to blueprint. I have a custom Pawn which has root Sphere, Static Mesh, Pawn Movement and Spline components (the latter is for the purpose of pathfollowing). It also has a behaviour tree.

When I place this custom Pawn to the world in editor from the c++ classes tab it works fine and does all what it should (navigates to random locations). However if I covert it to blueprint and launch the game it either does not find navigation goal (if I hit “Play”) or crashes the editor (if I hit “Simulate”)

If I debug this, it stops at the line

ensureMsgf(false, TEXT("Template Mismatch during attachment. Attaching instanced component to template component. Parent '%s' Self '%s'"), *Parent->GetName(), *GetName());

I use CreateDefaultSubobject function in the Pawn constructor for all components excepting BehaviorTree which I select in the editor.

Could you advice me something or point at the relevant direction? Thanks in advance.

Ok, looks like I found the issue here. If you use just CreateDefaultSubobject<>() while creating your component it causes it to be garbage-collected, so engine just deletes it and you get the crash. Looks like you should use the UPROPERTY() tag before your component declaration (before ALL of the components). With this tag included, your object is now properly initialized and don’t get “garbage-collected”. It worked for me.

I found (more detailed) explanation of it here: (thanks to cmartel) no clue why a very basic piece of code crashes - C++ - Epic Developer Community Forums