WidgetComponent Not Visible when added via C++

pretty simple… I can see it attached to the actor from the editor detail panel, and I can see the correct component transform and that my widget class is set in the interface, and the draw size is near the size of my actors width.

Here is the code:

AActorTest::AActorTest(const class FObjectInitializer& PCIP)
	: Super(PCIP)
	{
	this->bReplicates = true;
this->bAlwaysRelevant = true;

scene = PCIP.CreateDefaultSubobject<USceneComponent>(this, TEXT("SceneComp"));
if (scene)
	{
	RootComponent = scene;
	}

FStringClassReference MyWidgetClassRef(TEXT("/Game/Blueprints/UI/Debug/DebugCoords.DebugCoords_C"));

UWorld* world = GetWorld();
if (world) {
	APlayerController* pc = world->GetFirstPlayerController();

	if (pc && pc->IsLocalController())
		{
			if (UClass* MyWidgetClass = MyWidgetClassRef.TryLoadClass<UUserWidget>())
			{
				// Create the widget and store it.
				//DebugCoordsWidget = CreateWidget<UUserWidget>(pc, MyWidgetClass);

				widgetComponent = NewObject<UWidgetComponent>(this, TEXT("DebugCoords"));
				//if (!widgetComponent)
				//{
				//	ERROR("Could Not Create Widget Component!");
				//}
				widgetComponent->RegisterComponent();
				widgetComponent->AttachTo(scene);
				widgetComponent->SetWidgetSpace(EWidgetSpace::World);
				//widgetComponent->SetOwnerPlayer(pc->GetLocalPlayer());
				widgetComponent->SetWidgetClass(MyWidgetClass);
				widgetComponent->SetWorldRotation(FRotator(90, 0, 0));
				widgetComponent->SetWorldLocation(FVector(0, 0, 200));
				widgetComponent->SetMaxInteractionDistance(1.0f);
				
				
			}
			
		}
	}
	}

I thought that maybe it was because InitWidget() within WidgetComponent.cpp didnt call UpdateWidget(), but that didnt seem to do anything. Also, If i add a widgetcomponent in the design view of an actor from the editor, and set the same exact parameters i am setting in the code. it shows up as expected.

I also noticed that the widget component is not ticking, which is where it gets drawn to the render target. Trying to figure out why it isn’t ticking.

Not 100% sure, but i think setting visibility to true and registering last fixed it.

widgetComponent->AttachTo(scene);
				widgetComponent->SetWidgetSpace(EWidgetSpace::World);
				widgetComponent->SetOwnerPlayer(pc->GetLocalPlayer());
				widgetComponent->SetWidgetClass(MyWidgetClass);
				widgetComponent->SetWorldRotation(FRotator(90, 180, 0));
				widgetComponent->SetWorldLocation(FVector(0, 0, 200));
				widgetComponent->SetMaxInteractionDistance(1.0f);
				widgetComponent->SetPivot(FVector2D(0.5f, 0.5f));
				widgetComponent->SetVisibility(true);
				widgetComponent->RegisterComponent();

I know this is an old thread, but for anyone who has this issue, it was indeed because I had ticking disabled, because my UMG Widget didn’t need it, but the Widget Component does need it to draw the UMG Widget.

1 Like