# SetRootComponent directly after Spawn

**URL:** <https://forums.unrealengine.com/t/setrootcomponent-directly-after-spawn/449871>\
**Category:** Blueprint\
**Tags:** spawn, root-component, unreal-engine, UE4, CPP, question\
**Created:** [May 30, 2019, 5:27pm UTC](https://forums.unrealengine.com/t/setrootcomponent-directly-after-spawn/449871 "2019-05-30T17:27:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sydon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sydon](https://forums.unrealengine.com/u/Sydon)\
**Post date:** [May 30, 2019, 5:27pm UTC](https://forums.unrealengine.com/t/setrootcomponent-directly-after-spawn/449871/1 "2019-05-30T17:27:48Z")

</div>

### Attempt

I try to switch the RootComponent directly after spawning an Actor:

```
void ASpawner::BeginPlay()
{
	Super::BeginPlay();

	//Spawn
	UWorld* World = GetWorld();
	FActorSpawnParameters SpawnInfo;
	SpawnInfo.Owner = GetOwner();
	SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
	AActor* SpawnedActor = World->SpawnActor<AActor>(SpawnedClass, SpawnInfo);

	//Get attached Component
	UActorComponent* FoundComponent = SpawnedActor->GetComponentByClass(UPrimitiveComponent::StaticClass());
	UPrimitiveComponent* CastComponent = Cast<UPrimitiveComponent>(FoundComponent);

	//Set as Root
	SpawnedActor->SetRootComponent(CastComponent);
}

```

The spawned ActorBlueprint looks like this:

 ![278438-spawned.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/3/c/23c4420a8ccea80f816f161309fef463719d3a99.jpeg)

### Problem

I get the error message:  
Ensure condition failed: ParentActor != ActorPtr …  
Encountered an Actor attached to itself

### Question

Is this a bug or do I need to register the Actor/Component/etc.?

---

<div class="post-metadata">

**Author:** ![NorlinGames](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/norlingames/32/495614_2.png) [@NorlinGames](https://forums.unrealengine.com/u/NorlinGames)\
**Post date:** [May 30, 2019, 6:32pm UTC](https://forums.unrealengine.com/t/setrootcomponent-directly-after-spawn/449871/2 "2019-05-30T18:32:53Z")

</div>

I think you should set the RootComponent only in the actor’s constructor.

Also it’s not good at all to access an actor’s components from another actors.

There is a way to make some setup for a spawning actor before spawn is completed, [look hee for the upvoted answer](https://answers.unrealengine.com/questions/284744/spawning-an-actor-with-parameters.html#answer-469641). Though I’m sure it’s still not the way to setup the actor’s component. In most cases, components should be created in the actor’s constructor.

---

<div class="post-metadata">

**Author:** ![Sydon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sydon](https://forums.unrealengine.com/u/Sydon)\
**Post date:** [May 30, 2019, 7:59pm UTC](https://forums.unrealengine.com/t/setrootcomponent-directly-after-spawn/449871/3 "2019-05-30T19:59:54Z")

</div>

Interesting, that function was new to me.

Using it doesn’t trigger the ensure condition anymore, SetRootComponent returns a bool to indicate success, but it doesn’t do anything.

I know that it is bad practice to modify unknown Actors from outside. The reason I require this functionality is actually to autocorrect mistakes that inexperienced designers may make when choosing certain configurations. Since the projects are usually short-lived, it is easier to throw a warning and try to guess what the intention of the designer was.

Unfortunately, I don’t know how I could validate that SetRootComponent will crash by using Unreal’s functionality and checking for certain conditions doesn’t work since the involved entities don’t know about each other.
