# How to spawn actor in C++? Been trying for 9 hours now

**URL:** <https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411>\
**Category:** C++\
**Tags:** spawn, actor, unreal-engine, UE4, CPP, question\
**Created:** [November 4, 2015, 1:05pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411 "2015-11-04T13:05:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_73094a6c1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_73094a6c1](https://forums.unrealengine.com/u/anonymous_user_73094a6c1)\
**Post date:** [November 4, 2015, 1:05pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/1 "2015-11-04T13:05:25Z")

</div>

Can someone please show me a 5-10 step tutorial for spawning an actor properly according to standard Unreal Engine methodology? Are actors supposed to be spawned from player controller, character or actor? Is there a ten minute tutorial on youtube that shows you how to spawn an actor at FVector (0,0,0) when a key is pressed?

```
void AriseCPPPlayerController::OnPlaceObjectPressed()
{

//F KEY PRESS WORKS
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, ("At least the F key press works! Now let's spawn an actor."));

FActorSpawnParameters SpawnInfo;

FRotator myRot(0, 0, 0);
FVector myLoc(0, 0, 100);

//just spawn the god **** actor
AmySphere mySphere = GetWorld()->SpawnActor<AmySphere>(this->GetClass(), myLoc, myRot, SpawnInfo);

//AActor* MySpawnedActor = World->SpawnActor<AmyWallSegmentCPP>(myWallSegmentClass, SpawnLocation, SpawnRotation);
//AmyWallSegmentCPP* const SpawnedPickup = World->SpawnActor<AmyWallSegmentCPP>(myWallSegmentClass, SpawnLocation, SpawnRotation, SpawnParams);
}

```

How would I get the above code to work at the most basic level of Unreal Engine C++? Let’s say from a keypress triggered from player controller. I’m trying, I really am, I’m so tired.

---

<div class="post-metadata">

**Author:** ![phil\_me\_up](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@phil\_me\_up](https://forums.unrealengine.com/u/phil_me_up)\
**Post date:** [November 4, 2015, 1:50pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/2 "2015-11-04T13:50:59Z")

</div>

Try changing line 13 to something like

```
AmySphere* mySphere = GetWorld()->SpawnActor<AmySphere>(AmySphere::StaticClass(), myLoc, myRot, SpawnInfo);

```

Depending on what MySphere is, you may want to also setup the SpawnInfo to set collision / owners etc (more information here: [FActorSpawnParameters](https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/FActorSpawnParameters/index.html)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_33e74471](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_33e74471](https://forums.unrealengine.com/u/anonymous_user_33e74471)\
**Post date:** [November 4, 2015, 3:40pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/3 "2015-11-04T15:40:54Z")

</div>

Are you sure that the actor isn’t spawning? Aside from the compiler error on line 13 from trying to assign a pointer to a value, it should spawn. Are you sure it’s not spawning an empty actor that just doesn’t have any visuals attached to it?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_73094a6c1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_73094a6c1](https://forums.unrealengine.com/u/anonymous_user_73094a6c1)\
**Post date:** [November 4, 2015, 4:18pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/4 "2015-11-04T16:18:31Z")

</div>

I’m going to spend the entire day today trying to debug and find out the answer to your questions.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_73094a6c1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_73094a6c1](https://forums.unrealengine.com/u/anonymous_user_73094a6c1)\
**Post date:** [November 4, 2015, 4:24pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/5 "2015-11-04T16:24:58Z")

</div>

I am going to try again today.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_33e74471](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_33e74471](https://forums.unrealengine.com/u/anonymous_user_33e74471)\
**Post date:** [November 4, 2015, 5:33pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/6 "2015-11-04T17:33:01Z")

</div>

If you’re in VS you should be able to just plop a breakpoint in there, but if not you can throw in some logs everywhere using the stuff here:

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

```
 GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("This is an on screen message!"));

```

If you’re in the editor you should be able to look in the world outliner to see if your actors are popping up too.

---

<div class="post-metadata">

**Author:** ![Fallonsnest](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Fallonsnest](https://forums.unrealengine.com/u/Fallonsnest)\
**Post date:** [November 4, 2015, 7:48pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/7 "2015-11-04T19:48:56Z")

</div>

Also try to set the spawn info to Always spawn:

```
SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;

```

And like phil\_me\_up answered, first parameter of SpawnActor should be AmySphere::StaticClass()  
U are calling this from the player controller so using this-\>GetClass() as first parameter will spawn another player controller.

Here are some examples of spawning actors in UE4

```
FActorSpawnParameters SpawnInfo;
SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
auto SwatAIController = GetWorld()->SpawnActor<ASwatAIController>(SpawnInfo);

FActorSpawnParameters SpawnInfo;
SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
auto Equipment = GetWorld()->SpawnActor<ASwatEquipment>(DefaultInventoryClasses[i], FVector(0.0f), FRotator(0, 0, 0), SpawnInfo);
```

---

<div class="post-metadata">

**Author:** ![LostScout](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LostScout](https://forums.unrealengine.com/u/LostScout)\
**Post date:** [May 20, 2016, 7:40pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/8 "2016-05-20T19:40:47Z")

</div>

Try changing the parameters myLoc and myRot to &myLoc and &myRot. Those parameters are pointers and need an address.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_20ad62421](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_20ad62421](https://forums.unrealengine.com/u/anonymous_user_20ad62421)\
**Post date:** [October 23, 2016, 2:54pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/9 "2016-10-23T14:54:07Z")

</div>

It’s really this easy:

```
FVector Location(0.0f, 0.0f, 0.0f);
FRotator Rotation(0.0f, 0.0f, 0.0f);
FActorSpawnParameters SpawnInfo;
GetWorld()->SpawnActor<AProjectile>(Location, Rotation, SpawnInfo);

```

And don’t forget to `#include` the thing you’re trying to spawn.

---

<div class="post-metadata">

**Author:** ![OneCuil](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@OneCuil](https://forums.unrealengine.com/u/OneCuil)\
**Post date:** [May 31, 2017, 2:33pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/10 "2017-05-31T14:33:25Z")

</div>

Really basic question, where should this go? In the main actor’s script or is there a main that would be more appropriate?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_e815e8ea](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_e815e8ea](https://forums.unrealengine.com/u/anonymous_user_e815e8ea)\
**Post date:** [July 15, 2017, 3:08am UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/11 "2017-07-15T03:08:15Z")

</div>

What do you mean? This would go wherever it is you need to spawn something.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ed9d0a0d](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ed9d0a0d](https://forums.unrealengine.com/u/anonymous_user_ed9d0a0d)\
**Post date:** [January 21, 2018, 1:36pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/12 "2018-01-21T13:36:38Z")

</div>

Where would I put it if I want the actor to spawn at the start of a level?

---

<div class="post-metadata">

**Author:** ![Talad](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Talad](https://forums.unrealengine.com/u/Talad)\
**Post date:** [April 2, 2018, 10:35am UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/13 "2018-04-02T10:35:19Z")

</div>

Hi,  
I had the same problem, and I found two possible solutions.

1. If your actor is created in the Unreal Editor, then you can simply spawn it by code like this:

The pre-requisite is that your actor is replicated. Please note that actors deriving from AActor are not replicated by default, so you need to add in their constructor:

bReplicates = true;

or within the editor you should flag your actor as “Replicates”. It is one of the properties in the details panel.

1. If you want to create your actor completely within C++, and you have only the StaticMesh and the Material in the Editor (this was my case), you can create it like this:

Also in this case the pre-requisite is that your actor is replicated. Same case for case 1.

If your actor is invisible client side, means you didn’t replicate it, or you didn’t set the position properly. Please note the SetWorldLocation call despite the fact you are already giving the location in the spawnactor command. For me it works only if I call explicitely SetWorldLocation

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5eb152d8](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5eb152d8](https://forums.unrealengine.com/u/anonymous_user_5eb152d8)\
**Post date:** [May 1, 2018, 2:31pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/14 "2018-05-01T14:31:53Z")

</div>

I am totally new to UE4 and C++. I really would like to know where to put this. I just want to create several actors when the game begins. Is there a C++ file which is called at first? No infos in the internet, I am searching for 3 days now.

---

<div class="post-metadata">

**Author:** ![Fallonsnest](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Fallonsnest](https://forums.unrealengine.com/u/Fallonsnest)\
**Post date:** [May 2, 2018, 10:48am UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/15 "2018-05-02T10:48:39Z")

</div>

A good place would be your GameMode class.  
There is always just 1 instance of a game mode during gameplay, and it also only exist on the server during multiplayer games (which is resposible for spawning your actors)

If u want to spawn these actors when the game starts, u can use the StartPlay method

> **[AGameMode::StartPlay](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/GameFramework/AGameMode/StartPlay/)**
>
> Transitions to calls BeginPlay on actors.

---

<div class="post-metadata">

**Author:** ![namelessman](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/namelessman/32/1178487_2.png) [@namelessman](https://forums.unrealengine.com/u/namelessman)\
**Post date:** [April 8, 2019, 7:20am UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/16 "2019-04-08T07:20:43Z")

</div>

On beginplay

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b104db45](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b104db45](https://forums.unrealengine.com/u/anonymous_user_b104db45)\
**Post date:** [December 27, 2020, 3:35pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/17 "2020-12-27T15:35:44Z")

</div>

@phil_me_up could you give an insight on the idea behind “StaticClass” in EU4. In the templated function SpawnActor, we are already specifying the template type by AmySphere. Therefore we already say that we need an instance of this class. So what is a staticclass? Is it unreal way of saying an instance of the class?

---

<div class="post-metadata">

**Author:** ![SadMonsterParty](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sadmonsterparty/32/88708_2.png) [@SadMonsterParty](https://forums.unrealengine.com/u/SadMonsterParty)\
**Post date:** [December 1, 2021, 7:35pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/18 "2021-12-01T19:35:26Z")

</div>

Is it really that easy? or is this an obsolete solution? I’ve been trying for days to just spawn any object in any way, I will literally pay you to call me and walk me thru this.

---

<div class="post-metadata">

**Author:** ![SadMonsterParty](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sadmonsterparty/32/88708_2.png) [@SadMonsterParty](https://forums.unrealengine.com/u/SadMonsterParty)\
**Post date:** [December 1, 2021, 10:39pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/19 "2021-12-01T22:39:25Z")

</div>

oh god I included the .cpp rather than the .h

headdesk

thank you for the good solution; kaisellgren

now I can spawn things but they’re all at the same location as the first thing I spawn.

I’m also getting this error:

`LogActor: Warning: FloatingActor /Game/StarterContent/Maps/UEDPIE_0_Minimal_Default.Minimal_Default:PersistentLevel.FloatingActor_0 has natively added scene component(s), but none of them were set as the actor's RootComponent - picking one arbitrarily`

thoughts?

---

<div class="post-metadata">

**Author:** ![Horizonik](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Horizonik](https://forums.unrealengine.com/u/Horizonik)\
**Post date:** [November 7, 2022, 2:59pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411/20 "2022-11-07T14:59:56Z")

</div>

It means you didn’t declare a root component in your actor so it made one for you.

You just need to make a root component in your constructor:

```auto
// Setup root component
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));

// Attach your other component to the root (make it a child of the root component)
YourComponentNameHere->SetupAttachment(RootComponent);

```

[Next page](https://forums.unrealengine.com/t/how-to-spawn-actor-in-c-been-trying-for-9-hours-now/337411.md?page=2)
