Unreal Engine crashes in packaged build when creating an object

Hey folks,
I have a problem which I just can’t seem to fix. I went trough the whole internet and still wasn’t able to find a solution to my problem. I basically just want to create a PoceduralmeshComponent at runtime. My current method works just fine, however when I try it in an packaged build it just crashes. Here is my code:

for (int i = 0; i < Objects.Num(); i++) {
		ProcMesh = NewObject<UProceduralMeshComponent>(this, UProceduralMeshComponent::StaticClass());
		if (ProcMesh) {
			ProcMesh->RegisterComponent();
			ProcMeshComp.Add(ProcMesh);

			if (ProcMeshComp[i]) {
				ProcMeshComp[i]->AttachToComponent(RootComponent.Get(), FAttachmentTransformRules::KeepRelativeTransform);
				ProcMeshComp[i]->CreationMethod = EComponentCreationMethod::Instance;
}}}
There is no error message in the log, it just says "Fatal Error". However by bruteforcing my way trough I figured out that maybe "RegsisterComponent" is the problem. Please help me :)

why are you doing all those if checks? any particular reason?

Where are you calling the method? Is if from the Contructor or from BeginPlay?

If you are calling it from the constructor then you cannot use new. It must be in beginplay or called in it’s own function after beginplay runs.

Are you declaring the correct TArrays in your header?

UPROPERTY(BlueprintReadWrite, editAnywhere)
UProceduralMeshComponent* ProcMesh;

UPROPERTY(BlueprintReadWrite, editAnywhere)
	TArray<UProceduralMeshComponent*> ProcMeshComp;

UPROPERTY(BlueprintReadWrite, editAnywhere)
	TArray<UObject*> Objects;

Packed the project to windows and ran it with no errors

1 Like

Yeah, the second one is a little bit unnecessary. You do that in order to prevent a crash if anything went wrong in the component creation.

I am calling an own function from BeginPlay. I actually have these in my .h file:

UPROPERTY(VisibleDefaultsOnly, Instanced)
		UProceduralMeshComponent* ProcMesh;

	UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Instanced)
		TArray <UProceduralMeshComponent*> ProcMeshComp;

I will tick you answer as an solution if it works, thanks for the help.