title: Unreal Engine: How can I create UStaticMeshComponent in a loop?
body: <p>I am trying to create multiple <code>UStaticMeshComponent</code> by using for loop but Unreal Engine keeps triggering breakpoint on <code>CreateDefaultSubobject</code> in <code>Object.h</code>(Not in my code, it's from UE4 core API). When I'm creating a single component it's working fine. I am pretty new to UnrealEngine and C++ so I might be doing something dumb but please be easy on that:) </p>
  
  <p>Thank you so much for your help.</p>
  
  <p><strong>Header</strong></p>
  
  <pre><code>#pragma once
  
  #include "CoreMinimal.h"
  #include "GameFramework/Actor.h"
  #include "FlowSphere.generated.h"
  
  UCLASS()
  class CPP_PRACTICE_3_API AFlowSphere : public AActor
  {
      GENERATED_BODY()
  
  public: 
      // Sets default values for this actor's properties
      AFlowSphere();
  
  protected:
      // Called when the game starts or when spawned
      virtual void BeginPlay() override;
  
  public: 
      // Called every frame
      virtual void Tick(float DeltaTime) override;
  
  private:
      //UPROPERTY()
      //TArray&lt;UStaticMeshComponent*&gt; StaticMeshComponents;
  
      UStaticMeshComponent* test;
  
  };
  
  </code></pre>
  
  <p><strong>C++</strong></p>
  
  <pre><code>#include "FlowSphere.h"
  
  // Sets default values
  AFlowSphere::AFlowSphere()
  {
      int32 amount = 100;
  
      RootComponent = CreateDefaultSubobject&lt;USceneComponent&gt;("SceneComponent");
  
      for (int32 i = 0; i &lt; amount; i++) {
  
          //WHEN I INCLUDE THE LINE BELOW, UE4 START MAKING BREAKPOINT
          UStaticMeshComponent* item = CreateDefaultSubobject&lt;UStaticMeshComponent&gt;(TEXT("Sphere"));
          item.AttachTo(this-&gt;RootComponent);
  
      }
  
      //THIS WORKS FINE
      this-&gt;test = CreateDefaultSubobject&lt;UStaticMeshComponent&gt;(TEXT("HELLO")); 
      PrimaryActorTick.bCanEverTick = true;
  
  }
  
  // Called when the game starts or when spawned
  void AFlowSphere::BeginPlay()
  {
      Super::BeginPlay();
  
  }
  
  // Called every frame
  void AFlowSphere::Tick(float DeltaTime)
  {
      Super::Tick(DeltaTime);
  
  }
  </code></pre>
  
  <p><strong>Where it triggering breakpoint on the <code>Object.h</code> from UE4 API</strong></p>
  
  <pre><code>    /**
      * Create a component or subobject
      * @param    TReturnType                 class of return type, all overrides must be of this type
      * @param    SubobjectName               name of the new component
      * @param    bTransient                  true if the component is being assigned to a transient property. This does not make the component itself transient, but does stop it from inheriting parent defaults
      */
      template&lt;class TReturnType&gt;
      TReturnType* CreateDefaultSubobject(FName SubobjectName, bool bTransient = false)
      {
          UClass* ReturnType = TReturnType::StaticClass();
          return static_cast&lt;TReturnType*&gt;(CreateDefaultSubobject(SubobjectName, ReturnType, ReturnType, /*bIsRequired =*/ true, /*bIsAbstract =*/ false, bTransient));
      }
  </code></pre>