Calling AddComponent in Blueprint's ConstructionScript and C++'s OnContruction

Hello
To create procedural ladder, I made a blueprint and put AddStaticMeshComponent in ContructionScript. It works what i intended(Not complete yet though).


I tried to write the same logic as code in c++.

void ALadderBase::OnConstruction(const FTransform& Transform)
{
	Super::OnConstruction(Transform);
	int32 TruncatedZ = FMath::TruncToInt(GetActorScale3D().Z);
	if (TruncatedZ != HeightLevel)
	{
		HeightLevel = TruncatedZ;
		for (int32 i = 0; i < HeightLevel; i++)
		{
			UActorComponent* Created = AddComponentByClass(UStaticMeshComponent::StaticClass(), true, FTransform(), false);
			UStaticMeshComponent* Casted = Cast<UStaticMeshComponent>(Created);
			//To Debug, TArray<UStaticMeshComponent*> CreatedMeshes
			CreatedMeshes.Add(Casted);
			Casted->SetStaticMesh(LadderRungMesh);
			FVector Loc = FVector(0.0f, 0.0f, i);
			Casted->SetRelativeLocation(Loc);
			FVector Scal = FVector(1.0f, 1.0f, 1.0f) / GetActorScale3D();
			Casted->SetRelativeScale3D(Scal);
		}
	}
}

I thought that the c++'s code works as same as blueprint’s construction script, but when StaticMeshComponents are created, they all get TRASH_ in front of their name and I can’t see any meshes that were created.
In bluerpint’s ConstructionScript, only the created meshes that had been created at last Construction became TRASH_)
What am i missing?
Thank you

You might need to set the 2nd argument to false.

AddComponentByClass(UStaticMeshComponent::StaticClass(), false, FTransform(), false);

Unless you will call the AttachTo function yourself, that should work.

Still the created the StaticMeshComponents become TRASH_ immediately. :sob: Anyway Thank you for your reply