How to create component Dynamicly in 4.8?

TestMesh = ConstructObject(UStaticMeshComponent::StaticClass(), this, “”);
if (TestMesh)
{
TestMesh->RegisterComponent();
TestMesh->AttachTo(GetCapsuleComponent(), NAME_None);
TestMesh->SetStaticMesh(Cube);
TestMesh->SetRelativeLocation({ 200.f, 0.f, 0.f });
}
If I use this code, It will run successful, But the UBT show “warning C4996: ‘ConstructObject’: ConstructObject is deprecated. Use NewObject instead Please update your code to the new API before upgrading to the next release, otherwise your project will no longer compile.”

	TestMesh = NewNamedObject<UStaticMeshComponent>(this, FName("name"));
	if (TestMesh)
	{
		TestMesh->RegisterComponent();
		TestMesh->AttachTo(GetCapsuleComponent(), NAME_None);
		TestMesh->SetStaticMesh(Cube);
		TestMesh->SetRelativeLocation({ 200.f, 0.f, 0.f });
	}

If I using this code, When I stop the game the UE4 will crash.
So how to create component Dynamicly in 4.8? Please tell me! Thanks.

Try. “NewObject”

Also will crash.

what line is causing the crash , what debug error you get?

Assertion failed: (Index >= 0) & (Index < ArrayNum) [File:D:\BuildFarm\buildmachine_++depot+UE4-Releases+4.8\Engine\Source\Runtime\Core\Public\Containers\Array.h] [Line: 678]
Array index out of bounds: 0 from an array of size 0

KERNELBASE.dll {0x00007ff977028b9c} + 0 bytes
UE4Editor-Core.dll!FOutputDeviceWindowsError::Serialize() {0x00007ff95c747c0f} + 0 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.8\engine\sou
UE4Editor-Core.dll!FOutputDevice::Logf__VA() {0x00007ff95c6010b8} + 159 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.8\engine\sou
UE4Editor-Core.dll!FDebug::AssertFailed() {0x00007ff95c5d1e72} + 65 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.8\engine\sou
UE4Editor-Engine.dll!USceneComponent::OnComponentDestroyed() {0x00007ff950a0242d} + 141 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.8\engine\sou
UE4Editor-Engine.dll!UActorComponent::BeginDestroy() {0x00007ff9508f7608} + 0 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.8\engine\sou
UE4Editor-Engine.dll!UPrimitiveComponent::BeginDestroy() {0x00007ff95096b82e} + 0 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.8\engine\sou
UE4Editor-CoreUObject.dll!UObject::ConditionalBeginDestroy() {0x00007ff95c0a9264} + 0 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.8\engine\sou
UE4Editor-CoreUObject.dll!CollectGarbageInternal() {0x00007ff95c0a8bf8} + 0 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.8\engine\sou
UE4Editor-CoreUObject.dll!CollectGarbage() {0x00007ff95c0a8315} + 0 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.8\engine\sou

Today when I first play the game not crashed,and in the second time it crashed when I exit.

When I create the component twice or more, then exit the UE4 will crash

I changed second param. NewObject(this, “”)
and it run well.So what it means about the second param?

Ah, i got this problem before, you can’t have many component with the same name, you have to assign different name each time you create the component

Thank you very much!