I suspect that your UI Objects (vLayout, test etc) are Garbage collected…
You are creating new objects but you are storing the references about them on the stack. In doing so, at the end of your BeginPlay method, these newly created objects are not referenced by anyone anymore. Therefore, they are garbage collected and the ViewPort reference something which is not in memory anymore, leading to a crash.
Try creating class variables with UPROPERTY() referencing them and see if it solves the problem.
Something like that (in your header):
class MYPROJECT_API AMHUD : public AHUD
{
GENERATED_BODY()
private:
UPROPERTY()
UVerticalBox* vLayout;
UPROPERTY()
UButton* test;
UPROPERTY()
UVerticalBoxSlot* buttonSlot;
// Rest of the code
};
Also, attaching the debugger to your game could help track down the issue, thanks to the call stack leading to the crash.
Reading this doc:
https://docs.unrealengine.com/en-US/Programming/UnrealArchitecture/Objects/Optimizations (the section “Garbage Collection”)
And this doc: https://docs.unrealengine.com/en-US/Programming/UnrealArchitecture/Objects/Optimizations
should help you understand this process.
Edit: the thing making me not sure about that is the use of the “UWidget::TakeWidget()” method. Don’t know what it does about ownership…