'Creating Static Meshes in For Loop using C++ with unreal engine

this is the fix at top use this to create a string that Static Mesh Object can accept as a unique identifier so the engine doesn't crash

FName name = *FString::Printf(TEXT("Sphere %i"), i);

I want to use a for Loop to create static meshes that I can reference later and link to other events over OSC. I am having a problem figuring out how to name the instances using the CreateDefaultSubobject.

I get this error when Compiling my code:

S:\UnrealEngine\CPP_LEARNING\ShaderShootEm_000\Source\ShaderShootEm_000\FlowSphere.cpp(19): error C4456: declaration of 'i' hides previous local declaration 
S:\UnrealEngine\CPP_LEARNING\ShaderShootEm_000\Source\ShaderShootEm_000\FlowSphere.cpp(18): note: see declaration of 'i' 
S:\UnrealEngine\CPP_LEARNING\ShaderShootEm_000\Source\ShaderShootEm_000\FlowSphere.cpp(23): error C4800: Implicit conversion from 'int32' to bool. Possible information loss 
S:\UnrealEngine\CPP_LEARNING\ShaderShootEm_000\Source\ShaderShootEm_000\FlowSphere.cpp(23): note: consider using explicit cast or comparison to 0 to avoid this warning 
C:\Program Files\Epic Games\UE_5.0\Engine\Source\Runtime\Core\Public\HAL\Platform.h(998): note: see declaration of 'int32'

This is the Code

RootComponent = CreateDefaultSubobject<USceneComponent>("SceneComponent");
FVector CurrentLocation = GetActorLocation();
StaticMeshes.Init(NULL,10);
float distance = 0;
int32 i = 0;
 for(int32 i= 0; i < 5; i++)
 {
    FString Number = FString::FromInt(i);
    distance *= i * 100;
    StaticMeshes[i] = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Number %i"),i );
    StaticMeshes[i]->SetRelativeLocation(CurrentLocation + FVector(0.0f, 0.0f, distance));
    StaticMeshes[i]->SetupAttachment(RootComponent);
 }

thanks for any help in advance~



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source