# How to spawn a cube in C++?

**URL:** <https://forums.unrealengine.com/t/how-to-spawn-a-cube-in-c/406292>\
**Category:** Blueprint\
**Tags:** spawn, unreal-engine, cube, CPP, question\
**Created:** [October 6, 2017, 12:11pm UTC](https://forums.unrealengine.com/t/how-to-spawn-a-cube-in-c/406292 "2017-10-06T12:11:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zngocti](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@zngocti](https://forums.unrealengine.com/u/zngocti)\
**Post date:** [October 6, 2017, 12:11pm UTC](https://forums.unrealengine.com/t/how-to-spawn-a-cube-in-c/406292/1 "2017-10-06T12:11:21Z")

</div>

I need to spawn a few cubes to specific coordinates when I start the game. I want to do it with C++ but I can’t figure out how. All I found about this was outdated info or people using blueprints.

I hope someone can help me.

---

<div class="post-metadata">

**Author:** ![epernod](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@epernod](https://forums.unrealengine.com/u/epernod)\
**Post date:** [October 6, 2017, 12:40pm UTC](https://forums.unrealengine.com/t/how-to-spawn-a-cube-in-c/406292/2 "2017-10-06T12:40:24Z")

</div>

Hi,  
this answer should work: [https://answers.unrealengine.com/questions/579025/how-to-create-a-cube-manually-from-c-code.html](https://answers.unrealengine.com/questions/579025/how-to-create-a-cube-manually-from-c-code.html)

---

<div class="post-metadata">

**Author:** ![zngocti](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@zngocti](https://forums.unrealengine.com/u/zngocti)\
**Post date:** [October 7, 2017, 2:31am UTC](https://forums.unrealengine.com/t/how-to-spawn-a-cube-in-c/406292/3 "2017-10-07T02:31:12Z")

</div>

But this is not for spawning actors

---

<div class="post-metadata">

**Author:** ![epernod](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@epernod](https://forums.unrealengine.com/u/epernod)\
**Post date:** [October 8, 2017, 8:27am UTC](https://forums.unrealengine.com/t/how-to-spawn-a-cube-in-c/406292/4 "2017-10-08T08:27:49Z")

</div>

Ok, not sure to understand, here is my code to create a mesh: my class inherite from AActor. Maybe you can find a method that create automatically a cube geometry, otherwise here is how I draw myself the geometry:

```
ASofaVisualMesh::ASofaVisualMesh()
{
        mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
	RootComponent = mesh;
}

void ASofaVisualMesh::BeginPlay()
{
	Super::BeginPlay();

    TArray<FVector> vertices;
	TArray<int32> Triangles;
	TArray<FVector> normals;
	TArray<FVector2D> UV0;
	TArray<FProcMeshTangent> tangents;
	TArray<FLinearColor> vertexColors;

    // For a cube it should be something like:
    for(int i=0; i<8; ++i)
    {
            vertices.Add(FVector(..., ..., ...));
            normals.Add(....
    }

    for (int i = 0; i < 12; i++)
	{
               //create here the faces of the cube
	}
	
	mesh->CreateMeshSection_LinearColor(0, vertices, Triangles, normals, UV0, vertexColors, tangents, true);

	// Enable collision data
	mesh->ContainsPhysicsTriMeshData(true);
}
```

---

<div class="post-metadata">

**Author:** ![salm](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@salm](https://forums.unrealengine.com/u/salm)\
**Post date:** [October 8, 2017, 9:45am UTC](https://forums.unrealengine.com/t/how-to-spawn-a-cube-in-c/406292/5 "2017-10-08T09:45:46Z")

</div>

> **[Spawning Actors](https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/UnrealArchitecture/Actors/Spawning/)**
>
> Methods of creating new instances of Actors in gameplay code.

If you go into detail about what you are trying achieve and what is blocking you we’ll be able to help you more.
