Unresolved externals error with UChildActorComponent::CreateChildActor();

Hello,

So I’m trying to add components in C++, but in OnConstruction(), which is equivalent to the construction script in BP. I want to display temporary stuff by spawning some components whenever the user moves the BP actor in the scene.

Inside OnConstruction(), I have the following:

child = NewObject<UChildActorComponent>(this);
		
		if (child)
		{
			child->ChildActorClass = ATargetPoint::StaticClass();

			**child->CreateChildActor(); //<-------- problem here**

			child->AttachTo(RootComponent);

			child->RegisterComponent();

			child->bCreatedByConstructionScript = true;
		}

When I build, it throws this error:

error LNK2019: unresolved external symbol “public: void __cdecl UChildActorComponent::CreateChildActor(void)” (?CreateChildActor@UChildActorComponent@@QEAAXXZ) referenced in function “public: virtual void __cdecl ADilmunSplineSystem::OnConstruction(class FTransform const &)” (?OnConstruction@ADilmunSplineSystem@@UEAAXAEBVFTransform@@@Z)

Any Ideas why?

I’m guessing that CreateChildActor() is not implemented, just declared in its .h file. Also, it’s not defined as a virtual void (so that I should implement it somewhere). My other guess is that this function is deprecated.

I want to basically acquire the functionality of " add ChildComponentActor" blueprint node, in C++.

Thanks!

So, any help?

Did you manage to figure this out? I’m in the same boat as you. There seem to be a few threads on this topic but it’s not clear what the recommended C++ equivalent of AddChildComponentActor is.

Reposting my answer from a related thread:

"I had the same issue, below worked for me:

Instead of CreateChildActor (which doesn’t seem accessible with the default build modules) manually call OnComponentCreated like this childActorComponent->OnComponentCreated();

There seems to be a bug where this particular function is not automatically called in C++ alone (the blueprint node AddChildActorComponent works fine) and the ChildActor within your component ends up never being created unless you use the manual workaround.

See this thread for more details: https://answers.unrealengine.com/questions/41202/oncomponentcreated-not-called-when-using-createdef.html?sort=oldest "