# How can I spawn a sphere in C++?

**URL:** <https://forums.unrealengine.com/t/how-can-i-spawn-a-sphere-in-c/327430>\
**Category:** Blueprint\
**Tags:** spawn, sphere, unreal-engine, question\
**Created:** [August 2, 2015, 9:53pm UTC](https://forums.unrealengine.com/t/how-can-i-spawn-a-sphere-in-c/327430 "2015-08-02T21:53:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![i73](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@i73](https://forums.unrealengine.com/u/i73)\
**Post date:** [August 2, 2015, 9:53pm UTC](https://forums.unrealengine.com/t/how-can-i-spawn-a-sphere-in-c/327430/1 "2015-08-02T21:53:59Z")

</div>

Hey guys, I have been trying for a while now and I cannot seem to get this.  
It continuasly crashes with **: CreateDefultSubobject can only be used inside of UObject constructors. UObject constructing subobjects ca**

```
.CPP:
void AGameManager::SphereFactory()
{   
	SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
}
.h:
	USphereComponent* SphereComponent;

```

What and why is not letting me create this?

---

<div class="post-metadata">

**Author:** ![staticvoidlol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@staticvoidlol](https://forums.unrealengine.com/u/staticvoidlol)\
**Post date:** [August 3, 2015, 10:17am UTC](https://forums.unrealengine.com/t/how-can-i-spawn-a-sphere-in-c/327430/2 "2015-08-03T10:17:38Z")

</div>

This is a known limitation, and the error message is correctly pointing it out. I’m not sure why “CreateDefaultSubobject” is not allowed outside of the constructor.

I think you can use ContstructObject instead, e.g.

```
  //Create Component
  SphereComponent = ConstructObject<USphereComponent>(USphereComponent::StaticClass, this, TEXT("MySphereComponent"));
  if(SphereComponent) 
 {    
		// Register the component
		SphereComponent->RegisterComponent();
		
		// Attach to root component
		SphereComponent->AttachTo(GetRootComponent()); 
		  
		// or make it the rootcomponent - haven't tested this
		RootComponent = SphereComponent;
}
```

---

<div class="post-metadata">

**Author:** ![i73](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@i73](https://forums.unrealengine.com/u/i73)\
**Post date:** [February 10, 2017, 4:59am UTC](https://forums.unrealengine.com/t/how-can-i-spawn-a-sphere-in-c/327430/3 "2017-02-10T04:59:22Z")

</div>

Two years, working on it every single day, TWO LONG YEARS FINALLY!

```
ACell::ACell(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
 	// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;

	USphereComponent* box = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, FName("My Box"));
	box->bHiddenInGame = false;
	box->Mobility = EComponentMobility::Movable;
	RootComponent = box;

	UStaticMeshComponent* SphereMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
	SphereMesh->AttachTo(RootComponent);
	static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereMeshAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
	if (SphereMeshAsset.Succeeded()) {
		SphereMesh->SetStaticMesh(SphereMeshAsset.Object);
		SphereMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
		SphereMesh->SetWorldScale3D(FVector(0.8f));
	}
}

```

Nah just kidding, took me an hour once I learned c++.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_88d57b37](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_88d57b37](https://forums.unrealengine.com/u/anonymous_user_88d57b37)\
**Post date:** [August 20, 2017, 4:27pm UTC](https://forums.unrealengine.com/t/how-can-i-spawn-a-sphere-in-c/327430/4 "2017-08-20T16:27:14Z")

</div>

sorry, but the effort to spawn a simple sphere in unreal from code is just ridicoulous …

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_3d10c415](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_3d10c415](https://forums.unrealengine.com/u/anonymous_user_3d10c415)\
**Post date:** [August 24, 2017, 4:51am UTC](https://forums.unrealengine.com/t/how-can-i-spawn-a-sphere-in-c/327430/5 "2017-08-24T04:51:47Z")

</div>

BTW, you did not spawned sphere but created actor with sphere component and mesh component

---

<div class="post-metadata">

**Author:** ![Dexgecko](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dexgecko](https://forums.unrealengine.com/u/Dexgecko)\
**Post date:** [July 23, 2018, 6:00am UTC](https://forums.unrealengine.com/t/how-can-i-spawn-a-sphere-in-c/327430/6 "2018-07-23T06:00:02Z")

</div>

FYI, you can reference the engine sphere at StaticMesh’/Engine/BasicShapes/Sphere.Sphere’

---

<div class="post-metadata">

**Author:** ![sandorvasas](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@sandorvasas](https://forums.unrealengine.com/u/sandorvasas)\
**Post date:** [January 20, 2019, 7:38pm UTC](https://forums.unrealengine.com/t/how-can-i-spawn-a-sphere-in-c/327430/7 "2019-01-20T19:38:14Z")

</div>

If I put the above code in a loop with \>2 iterations, it crashes the editor…

---

<div class="post-metadata">

**Author:** ![SuryaPrakashModi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SuryaPrakashModi](https://forums.unrealengine.com/u/SuryaPrakashModi)\
**Post date:** [May 25, 2021, 7:20am UTC](https://forums.unrealengine.com/t/how-can-i-spawn-a-sphere-in-c/327430/8 "2021-05-25T07:20:43Z")

</div>

watch this. [How To Do Line Tracing Using C++ In Unreal Engine - YouTube](https://youtu.be/LWeQicz_teo)
