# Dynamically spawned UActorComponents disappear when parent is moved in editor

**URL:** <https://forums.unrealengine.com/t/dynamically-spawned-uactorcomponents-disappear-when-parent-is-moved-in-editor/306397>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [February 9, 2015, 6:14am UTC](https://forums.unrealengine.com/t/dynamically-spawned-uactorcomponents-disappear-when-parent-is-moved-in-editor/306397 "2015-02-09T06:14:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Backov](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Backov](https://forums.unrealengine.com/u/Backov)\
**Post date:** [February 9, 2015, 6:14am UTC](https://forums.unrealengine.com/t/dynamically-spawned-uactorcomponents-disappear-when-parent-is-moved-in-editor/306397/1 "2015-02-09T06:14:08Z")

</div>

I have an Actor (a spaceship) that spawns its turrets dynamically at runtime. It uses the following code to do so, and works:

```
UChildActorComponent *child = NewObject<UChildActorComponent>(AttachedToPawn);

if (child)
{
	UObject *gCDO = itemClass->GeneratedClass->GetDefaultObject();

	child->ChildActorClass = TSubclassOf<AActor>(gCDO->GetClass());
	child->OnComponentCreated();
	child->AttachTo(AttachComponent, tmd->MountName);
	child->bCreatedByConstructionScript = true;
	child->RegisterComponent();

	ATurretActor *turret = Cast<ATurretActor>(child->ChildActor);

	turret->SetTurretData(tvd, GameData);
	turret->MountData = tmd;
	Turrets.Add(turret);
}

```

The bit with GetDefaultObject() is a bit messy, but I couldn’t find any way to convert a `TSubclassOf(UObject)` to `TSubclassOf(AActor)`, which is the only thing ChildActorClass will accept.

Now - this code spawns the turrets and they attach to the right places and when the ship moves, they move with it. All working.

However, if I eject, grab the ship pawn and move it around manually (any amount) all of the dynamic turrets despawn. I have a UChildActorComponent that I added to the Ship manually in the ships blueprint, and it doesn’t despawn.

So there’s obviously something I am missing here. What is it?

---

<div class="post-metadata">

**Author:** ![MickD777](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MickD777](https://forums.unrealengine.com/u/MickD777)\
**Post date:** [February 9, 2015, 10:24pm UTC](https://forums.unrealengine.com/t/dynamically-spawned-uactorcomponents-disappear-when-parent-is-moved-in-editor/306397/2 "2015-02-09T22:24:47Z")

</div>

It’s because of this line :

```
child->bCreatedByConstructionScript = true;

```

Each time you will modify a value or move the actor around, all components created by the construction script (or flagged as so) will be destroyed. Usually that kind of behavior is wanted since you do the spawning in the OnConstruction() function and so it will respawn anyway.

If that’s not your case, you probably want to skip that line.

---

<div class="post-metadata">

**Author:** ![Backov](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Backov](https://forums.unrealengine.com/u/Backov)\
**Post date:** [February 14, 2015, 2:21am UTC](https://forums.unrealengine.com/t/dynamically-spawned-uactorcomponents-disappear-when-parent-is-moved-in-editor/306397/3 "2015-02-14T02:21:30Z")

</div>

Thanks, that’s got it and a good piece of info
