# (theoretically) valid cast failed

**URL:** <https://forums.unrealengine.com/t/theoretically-valid-cast-failed/483184>\
**Category:** Platform & Builds\
**Tags:** casting, cast-failed, unreal-engine, cast, question\
**Created:** [May 10, 2021, 5:30pm UTC](https://forums.unrealengine.com/t/theoretically-valid-cast-failed/483184 "2021-05-10T17:30:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xilef\_HD](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Xilef\_HD](https://forums.unrealengine.com/u/Xilef_HD)\
**Post date:** [May 10, 2021, 5:30pm UTC](https://forums.unrealengine.com/t/theoretically-valid-cast-failed/483184/1 "2021-05-10T17:30:40Z")

</div>

So basically I am trying to load a widget from an UGC package. But when I try to cast the widgetclass to its superclass, the cast just returns NULL.

relevant cpp code excerpt:

```
for (FUGCPackage& package : UGCRegistry->UGCPackages) {
	TArray<UClass*> Classes;
	UGCRegistry->GetAllClassesInPackage(package, Classes);

	for (UClass* currentClass : Classes) {
		if (currentClass->IsChildOf(UAppIconSuperclass::StaticClass())) {
			if ("appIcon_C" == UKismetSystemLibrary::GetClassDisplayName(currentClass)) {

				if (Cast<UAppIconSuperclass>(currentClass) != NULL) {
					GEngine->AddOnScreenDebugMessage(-1, 100, FColor::Green, "valid"); // doesn't print -> cast failed
				}
				if (currentClass) {
					GEngine->AddOnScreenDebugMessage(-1, 100, FColor::Green, "was valid"); // does print -> class is valid
				}

```

I know that the class has got UAppIconSuperclass as parent, and first if check let’s it through, so why does the cast fail? I also tried to cast to UUserWidget (parent class of UAppIconSuperclass) - with the same result…

Is anyone able to help me and tell me what I am doing wrong / why it wouldn’t cast correctly?

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [May 11, 2021, 3:41pm UTC](https://forums.unrealengine.com/t/theoretically-valid-cast-failed/483184/2 "2021-05-11T15:41:12Z")

</div>

Hello! **currentClass** is **UClass** , not **UAppIconSuperclass** child!

---

<div class="post-metadata">

**Author:** ![Xilef\_HD](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Xilef\_HD](https://forums.unrealengine.com/u/Xilef_HD)\
**Post date:** [May 11, 2021, 5:16pm UTC](https://forums.unrealengine.com/t/theoretically-valid-cast-failed/483184/3 "2021-05-11T17:16:32Z")

</div>

but currentClass-\>GetSuperClass()-\>GetName() returns AppIconSuperclass. so why is it just a UClass now and how would I change that / make it work?

---

<div class="post-metadata">

**Author:** ![Xilef\_HD](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Xilef\_HD](https://forums.unrealengine.com/u/Xilef_HD)\
**Post date:** [May 13, 2021, 5:31pm UTC](https://forums.unrealengine.com/t/theoretically-valid-cast-failed/483184/4 "2021-05-13T17:31:08Z")

</div>

nvmd. solved it myself
