Component created with NewObject is invisible

maybe it’s visible, but spawned in the origin of the world? try text_render->AttachToComponent(…);

I was creating a sub-object inside my actor’s constructor the usual way:

AMyActor::AMyActor(const FObjectInitializer &init) : Super(init)
    text_render = CreateDefaultSubobject<UTextRenderComponent>(TEXT("info_text"));

Worked fine. Then I needed to move this creation into a member object of the actor and do this creation outside the constructor. The recommended approach was to use NewObject:

    text_render = NewObject<UTextRenderComponent>(m_owner, TEXT("info_text"));

This compiles and runs and creates the component, but it’s invisible. It seems like that second parameter is wrong – it should be a “UClass” instead of a text string. But what “UClass” would I use for a text render component?

Followup: I tried passing UTextRenderComponent::StaticClass() as the second argument, which also compiles and runs, but the component is still invisible.

In both cases, I can see all the properties of the UTextRenderComponent in the editor, under “Details.” All of the object’s properties look correct. And, under “Rendering,” the “Visible” checkbox is checked.

The attachment and transform are the same in both cases.
Something interesting, when I double-click the object in the hierarchy, it doesn’t focus the camera on it, like it can’t find it.

Got exactly same problem, solved with RegisterComponent:

UParticleSystemComponent* psc = NewObject<UParticleSystemComponent(OwnerActor);
if (psc) {

I ran into a similar issue that was solved by this answer – turns out it was a weird matter of CDOs. The UStaticMeshComponent (and/or the UStaticMesh) created in the ctor of the component I was trying to create/add at runtime only worked for one instance, presumably because the CDO paradigm has the Component constructor only ever called once; all future instances were present but invisible. I moved the UStaticMeshComponent and other init things from the ctor to BeginPlay() and everything worked nicely!

I don’t understand why the subsequent instances couldn’t re-use the mesh data, though. Even assuming each instance was pairing the same mesh with its own unique transform should have meant that only the latest instance was visible, rather than only the first. I’ve asked my own question about that!

1 Like