# Add Replicated Components in Runtime Problem

**URL:** <https://forums.unrealengine.com/t/add-replicated-components-in-runtime-problem/17726>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [December 24, 2014, 2:06am UTC](https://forums.unrealengine.com/t/add-replicated-components-in-runtime-problem/17726 "2014-12-24T02:06:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dzhyrma](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dzhyrma](https://forums.unrealengine.com/u/Dzhyrma)\
**Post date:** [December 24, 2014, 2:06am UTC](https://forums.unrealengine.com/t/add-replicated-components-in-runtime-problem/17726/1 "2014-12-24T02:06:06Z")

</div>

Hi Guys!

I am trying to add components to my replicated actor. First I’ve added a replicated scene as a RootComponent. It worked fine. I’ve seen this scene on the client also, but when I am trying to create a new mesh component, attach it to this scene and set it as replicated, I get an error in logs and then client restarts. This is my error:  
LogNetTraffic:Error: ReadContentBlockHeader: Stably named sub-object not found. Actor: GameGeneratedActor\_7  
LogNet:Error: UActorChannel::ReceivedBunch: ReadContentBlockHeader FAILED. Bunch.IsError() == TRUE. Closing connection.

Here is my code for creating an actor (I just simply place this actor on the map):

```auto

AGameGeneratedActor::AGameGeneratedActor(const class FObjectInitializer& PCIP)
: Super(PCIP), objectInitializer(PCIP)
{
	if (Role < ROLE_Authority){
		UE_LOG(GamoraLog, Warning, TEXT("CLIENT : CREATING AN OBJECT"));
	}
	else {
		UE_LOG(GamoraLog, Warning, TEXT("SERVER : CREATING AN OBJECT"));
	}
	RootComponent = PCIP.CreateDefaultSubobject<USceneComponent>(this, TEXT("DefaultScene"));
	RootComponent->OnComponentCreated();
	RootComponent->RegisterComponent();
	RootComponent->SetIsReplicated(true);
	bNetLoadOnClient = false;
	bReplicates = true;
}

```

In another function, which I call from the server to create a new component:

```auto

		class UMeshComponent* meshC = objectInitializer.CreateDefaultSubobject<UMeshComponent>(this, TEXT("SomeMesh"));
		meshC->AttachTo(RootComponent);
		meshC->OnComponentCreated();
		meshC->RegisterComponent();
		meshC->SetIsReplicated(true);

```

Note: When I move the code for component creation to the constructor, it works.

Can you help me, what am I doing wrong, and how can I create components in runtime with replication?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a61eb923](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a61eb923](https://forums.unrealengine.com/u/anonymous_user_a61eb923)\
**Post date:** [July 26, 2017, 3:29pm UTC](https://forums.unrealengine.com/t/add-replicated-components-in-runtime-problem/17726/2 "2017-07-26T15:29:42Z")

</div>

I had the same problem , I couldn’t still find a solution. (Unreal engine 4.16)

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [July 27, 2017, 8:31am UTC](https://forums.unrealengine.com/t/add-replicated-components-in-runtime-problem/17726/3 "2017-07-27T08:31:11Z")

</div>

You need to use NewObject to create components at runtime, you can’t store the Object Initializer to create objects (it’s a reference anyway, so you can’t even guarantee it exists)

```auto

UMyComponent* NewComponent = NewObject<UMyComponent>(PointerToActor, MyComponent::StaticClass());
NewComponent ->RegisterComponent();
// Now you can do stuff
// The component will replicate if it has SetIsReplicated(true) in it's constructor.

```

I create weapons, inventory etc. this way and have done for yonks. Replication works as normal!
