# Destorying a slate widget inside a UWidget class(custom Widget component)

**URL:** <https://forums.unrealengine.com/t/destorying-a-slate-widget-inside-a-uwidget-class-custom-widget-component/53544>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [January 26, 2016, 11:05am UTC](https://forums.unrealengine.com/t/destorying-a-slate-widget-inside-a-uwidget-class-custom-widget-component/53544 "2016-01-26T11:05:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![envenger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@envenger](https://forums.unrealengine.com/u/envenger)\
**Post date:** [January 26, 2016, 11:05am UTC](https://forums.unrealengine.com/t/destorying-a-slate-widget-inside-a-uwidget-class-custom-widget-component/53544/1 "2016-01-26T11:05:25Z")

</div>

Hello everyone,

I am following Rama guide on creating custom Widget component in slate that can be used in UMG

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

```auto

TSharedRef<SWidget> UBaseSpellUMGWidget::RebuildWidget()
{
	SpellUI = SNew(SBaseSpellSlateWidget)
		.UMGWidgetPtr(this);

	return SpellUI.ToSharedRef();
}

```

I am creating the slate widget like this in code.

The problem is at a later stage, I need to destroy the Slate widget and create another slate widget at its location and I am not able to do so.

```auto

	if (SpellUI.IsValid())
	{
// ReleaseSlateResources(true);
// SpellUI.Reset();
// SpellUI = nullptr;
// 		
 		// New Spell
 		SAssignNew(SpellUI, SBaseSpellSlateWidget)
			.UMGWidgetPtr(this);

 		SpellUI->SetSpell(_BaseSpell);
	}

```

I have tried multiple ways to try to delete it but it seems the widget persists no matter what. And if I replace the pointer with another widget, the behavior is getting weird. I am sure no other SharedPtr point to that widget.

So is there anyway to destroy that widget so I can create another widget in that location?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4fe9f58b1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4fe9f58b1](https://forums.unrealengine.com/u/anonymous_user_4fe9f58b1)\
**Post date:** [January 26, 2016, 4:53pm UTC](https://forums.unrealengine.com/t/destorying-a-slate-widget-inside-a-uwidget-class-custom-widget-component/53544/2 "2016-01-26T16:53:19Z")

</div>

UMG builds the Slate layout, calling RebuildWidget on the entire hierarchy, then Slate owns the actual SWidget returned by your UMG wrapper. If you update the pointer held by UMG, then UMG has a new widget but Slate doesn’t.

You could fix this by manually rebuilding the widget yourself, but an even simpler solution would be to wrap your widget with a container like SBox, and instead have your UMG wrapper change the box’s child widget.

```auto

TSharedRef<SWidget> UBaseSpellUMGWidget::RebuildWidget()
{
	SAssignNew( WidgetBox, SBox )
	
		SNew( SBaseSpellSlateWidget )
		.UMGWidgetPtr( this );
	]

	return WidgetBox.ToSharedRef();
}

if (WidgetBox.IsValid())
{
	WidgetBox->SetContent( 
		SNew( SBaseSpellSlateWidget )
		.UMGWidgetPtr( this )
	);

	SpellUI->SetSpell( _BaseSpell) ;
}

```

-

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [January 26, 2016, 4:53pm UTC](https://forums.unrealengine.com/t/destorying-a-slate-widget-inside-a-uwidget-class-custom-widget-component/53544/3 "2016-01-26T16:53:54Z")

</div>

Pretty sure you’re just not really supposed to do that. The framework calls RebuildWidget at particular times, and then wraps the resulting slate widget and fits it into the UMG hierarchy. If you try to recreate it by yourself outside of that sequence, you’re asking for trouble.

Is there some reason you can’t just modify the configuration of the existing widget instead?

Edit: This was in response to original post. Just do what said. 🙂

---

<div class="post-metadata">

**Author:** ![envenger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@envenger](https://forums.unrealengine.com/u/envenger)\
**Post date:** [January 27, 2016, 8:33am UTC](https://forums.unrealengine.com/t/destorying-a-slate-widget-inside-a-uwidget-class-custom-widget-component/53544/4 "2016-01-27T08:33:55Z")

</div>

> [@cmartel;461442](#):
>
> UMG builds the Slate layout, calling RebuildWidget on the entire hierarchy, then Slate owns the actual SWidget returned by your UMG wrapper. If you update the pointer held by UMG, then UMG has a new widget but Slate doesn’t.
> 
> You could fix this by manually rebuilding the widget yourself, but an even simpler solution would be to wrap your widget with a container like SBox, and instead have your UMG wrapper change the box’s child widget.
> 
> ```auto
> 
> TSharedRef<SWidget> UBaseSpellUMGWidget::RebuildWidget()
> {
> SAssignNew( WidgetBox, SBox )
> 	
> SNew( SBaseSpellSlateWidget )
> .UMGWidgetPtr( this );
> ]
> 
> return WidgetBox.ToSharedRef();
> }
> 
> if (WidgetBox.IsValid())
> {
> WidgetBox->SetContent( 
> SNew( SBaseSpellSlateWidget )
> .UMGWidgetPtr( this )
> );
> 
> SpellUI->SetSpell( _BaseSpell) ;
> }
> 
> ```
> 
> -

Okay this solution worked… No idea why I had not thought about that.

Thank you…
