# my root widget slot is null

**URL:** <https://forums.unrealengine.com/t/my-root-widget-slot-is-null/451959>\
**Category:** UI\
**Tags:** unreal-engine, slate, Widget, UE4, CPP, Blueprint, question\
**Created:** [July 9, 2019, 1:53pm UTC](https://forums.unrealengine.com/t/my-root-widget-slot-is-null/451959 "2019-07-09T13:53:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SatanicDeveloper](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SatanicDeveloper](https://forums.unrealengine.com/u/SatanicDeveloper)\
**Post date:** [July 9, 2019, 1:53pm UTC](https://forums.unrealengine.com/t/my-root-widget-slot-is-null/451959/1 "2019-07-09T13:53:46Z")

</div>

I created a c++ class based on umg and i want to add a text block to it . first i get the root widget and check that if it is null add a root widget but when i want to cast slot of my root widget , the slot is null and my cast fails . also i tried adding manually a root component to Blueprint of my c++ widget and my text block wont appear in hierarchy .

TSharedRef UW\_Player::RebuildWidget()  
{

```
UPanelWidget* RootWidget = Cast<UPanelWidget>(GetRootWidget());

if (!RootWidget)
{
	RootWidget = WidgetTree->ConstructWidget<UCanvasPanel>(UCanvasPanel::StaticClass(), TEXT("RootWidget"));

	UCanvasPanelSlot* RootWidgetSlot = Cast<UCanvasPanelSlot>(RootWidget->Slot);//Slot Is Null 
	if (RootWidgetSlot)
	{
		RootWidgetSlot->SetAnchors(FAnchors(0.0f, 0.0f, 1.0f, 1.0f));
		RootWidgetSlot->SetOffsets(FMargin(0.0f, 0.0f));

	}

	WidgetTree->RootWidget = RootWidget;

}

TSharedRef<SWidget> Widget = Super::RebuildWidget();

if (RootWidget && WidgetTree)
{
	HealthText = WidgetTree->ConstructWidget<UTextBlock>(UTextBlock::StaticClass(), TEXT("HealthText"));

	RootWidget->AddChild(HealthText);
	UCanvasPanelSlot* HealthTextSlot = Cast<UCanvasPanelSlot>(HealthText->Slot);
	if (HealthTextSlot)	
	{
		HealthTextSlot->SetAutoSize(true);

	}

}

return Widget;

```

}

 ![281973-and-here-is-with-root.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/6/e/e6ef527d0509506018b5fee600fa71be0de66c59.png)
