Hi All,
I’m experiencing an issue where I am periodically calling a method to create new StaticMeshComponents at different locations all belonging to one actor, however meshes are later disappearing.
I can’t find anything wrong in my code, but I must be missing something, or misunderstanding some element of this process - I just don’t know what.
// breaks after a certain amount of times
for (int i = 0; i < ownedLand.Num(); i++)
{
FSurfaceLocation& sl = ownedLand[i];
if (sl.industryOccupied == EIndustry::Empty)
{
sl.industryOccupied = EIndustry::ResourceGathering;
FString nameString = FString::Printf(TEXT("IndustryMeshComponent %d %d"), sl.SurfaceCoord.X, sl.SurfaceCoord.Y);
FName name = FName(*nameString);
sl.MeshComponent = NewObject<UStaticMeshComponent>(this, name);
sl.MeshComponent->RegisterComponent();
sl.MeshComponent->SetMobility(EComponentMobility::Movable);
sl.MeshComponent->SetupAttachment(Root);
sl.MeshComponent->SetStaticMesh(LoadObject<UStaticMesh>(nullptr, TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere")));
sl.MeshComponent->SetWorldLocation(sl.VoxelHit.Hit.Location);
sl.MeshComponent->SetWorldScale3D(FVector(2, 2, 2));
}
}
Seemingly at random, some of the older meshes are disapearing when the method is re-executed.
Here is my declartion for the FSurfaceLocation struct:
USTRUCT(BlueprintType)
struct FSurfaceLocation
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Location")
FVoxelProjectionHit VoxelHit;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Location")
FVector2D SurfaceCoord;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Location")
FVector AbovePoint;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Location")
EIndustry industryOccupied = EIndustry::Empty;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Location")
UStaticMeshComponent* MeshComponent = nullptr;
};
I’ve confirmed that I’m not somehow overriding them:
if (!sl.MeshComponent)
{
...
sl.MeshComponent = NewObject<UStaticMeshComponent>(this, name);
}
Any ideas why meshes are disappearing?
Thanks