# How to create many UStaticMeshComponent's with a for loop?

**URL:** <https://forums.unrealengine.com/t/how-to-create-many-ustaticmeshcomponents-with-a-for-loop/424248>\
**Category:** C++\
**Tags:** for-loop, unreal-engine, UE4, question, CPP\
**Created:** [May 3, 2018, 3:30pm UTC](https://forums.unrealengine.com/t/how-to-create-many-ustaticmeshcomponents-with-a-for-loop/424248 "2018-05-03T15:30:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ktbu](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ktbu](https://forums.unrealengine.com/u/Ktbu)\
**Post date:** [May 3, 2018, 3:30pm UTC](https://forums.unrealengine.com/t/how-to-create-many-ustaticmeshcomponents-with-a-for-loop/424248/1 "2018-05-03T15:30:40Z")

</div>

UStaticMeshComponent\* Spike = CreateDefaultSubobject(TEXT(“Spike”));  
Spike-\>GetComponentLocation() = NewLocation;  
static ConstructorHelpers::FObjectFinder SpikeAsset(TEXT(“StaticMesh’/Game/StarterContent/Shapes/Shape\_Cone.Shape\_Cone’”));

```
	UStaticMeshComponent* Spikes = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Spikes"));

	for (int x = 0; x<10; x++){

		//I want to create 10 extra spikes with the loop, but how?
		UStaticMeshComponent* Spikes = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Spikes"));
		//Attaching them to my root component
		Spikes->SetupAttachment(Spike);
		//Moving them 100 x
		Spikes->SetRelativeLocation(NewLocation += FVector(100, 0, 0));
		
		if (SpikeAsset.Succeeded()) {
			Spike->SetStaticMesh(SpikeAsset.Object);
			//adding shape to all my units
			Spikes->SetStaticMesh(SpikeAsset.Object);
		}
	}

```

What I basically want to do here is create 10 more spikes with a for loop.

How do I add 10 static mesh components with a for loop?

---

<div class="post-metadata">

**Author:** ![xSheim](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@xSheim](https://forums.unrealengine.com/u/xSheim)\
**Post date:** [May 3, 2018, 5:57pm UTC](https://forums.unrealengine.com/t/how-to-create-many-ustaticmeshcomponents-with-a-for-loop/424248/2 "2018-05-03T17:57:03Z")

</div>

You will have to create an array of components, take a look: [C++ - array of mesh components, how to - Programming & Scripting - Unreal Engine Forums](https://answers.unrealengine.com/questions/18350/c-array-of-mesh-components-how-to.html?sort=oldest)
