# FindWidget Crashes Unreal Engine

**URL:** <https://forums.unrealengine.com/t/findwidget-crashes-unreal-engine/376225>\
**Category:** UI\
**Tags:** crash-editor, unreal-engine, find, Widget, question\
**Created:** [November 14, 2016, 7:07am UTC](https://forums.unrealengine.com/t/findwidget-crashes-unreal-engine/376225 "2016-11-14T07:07:13Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_f6022ca0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f6022ca0](https://forums.unrealengine.com/u/anonymous_user_f6022ca0)\
**Post date:** [November 14, 2016, 7:07am UTC](https://forums.unrealengine.com/t/findwidget-crashes-unreal-engine/376225/1 "2016-11-14T07:07:13Z")

</div>

I have a custom UserWidget called GameWidget

code is:

```
//GameWidget.h
#pragma once

#include "Runtime/UMG/Public/UMG.h"
#include "Runtime/UMG/Public/UMGStyle.h"
#include "Runtime/UMG/Public/Slate/SObjectWidget.h"
#include "Runtime/UMG/Public/IUMGModule.h"
#include "Runtime/UMG/Public/Blueprint/UserWidget.h"
#include "Blueprint/UserWidget.h"
#include "GameWidget.generated.h"

/**
 * 
 */
UCLASS()
class SPACESHOOTER2_API UGameWidget : public UUserWidget
{
	GENERATED_BODY()
	
public:
	void Load();

	void SetScore(int score);
	void OnGameOver(int score);

	UPROPERTY()
	UTextBlock* ScoreText;
	
	
};

//GameWIdget.cpp
#include "SpaceShooter2.h"
#include "GameWidget.h"

void UGameWidget::Load()
{
	const FName TextBlockName = FName(TEXT("GameTextBlock"));

	if (ScoreText == nullptr) {
		//ScoreText = (UTextBlock*)(WidgetTree->FindWidget(TextBlockName));
		WidgetTree->FindWidget(TextBlockName);
	}
}

void UGameWidget::SetScore(int score)
{
	if (ScoreText != nullptr) {
		ScoreText->SetText(FText::FromString(FString(TEXT("Score: ")) + FString::FromInt(score)));
	}
}

void UGameWidget::OnGameOver(int score)
{
	if (ScoreText != nullptr)
	{
		ScoreText->SetText(FText::FromString(FString(TEXT("Press R To Restart\nScore: ")) + FString::FromInt(score) ));
	}
}

```

Here is what the blueprint made from the class GameWidget looks like:

 ![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/3/5/d352344aed3716148cb26a3f77215f32b696e271.jpeg)  
Calling ‘Load’ causes the crash.  
I narrowed it down specifically to calling ‘FindWidget’

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f6022ca0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f6022ca0](https://forums.unrealengine.com/u/anonymous_user_f6022ca0)\
**Post date:** [November 14, 2016, 7:37am UTC](https://forums.unrealengine.com/t/findwidget-crashes-unreal-engine/376225/2 "2016-11-14T07:37:15Z")

</div>

Yeah, I also tried GetAllWidgets and THAT causes a crash as well. This is seriously frustrating, I’m just trying to get through a tutorial.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2b03d27c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2b03d27c](https://forums.unrealengine.com/u/anonymous_user_2b03d27c)\
**Post date:** [November 14, 2016, 2:47pm UTC](https://forums.unrealengine.com/t/findwidget-crashes-unreal-engine/376225/3 "2016-11-14T14:47:40Z")

</div>

Hey Gochida-

Looking at the code provided, the likely issue is that WidgetTree is null when you reference it which causes the crash. Can you try adding `if(WidgetTree)` prior to the line where you call FindWidget(). If you are still getting a crash, please provide the callstack and log files from the crash for more information.

Cheers

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f6022ca0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f6022ca0](https://forums.unrealengine.com/u/anonymous_user_f6022ca0)\
**Post date:** [November 14, 2016, 8:38pm UTC](https://forums.unrealengine.com/t/findwidget-crashes-unreal-engine/376225/4 "2016-11-14T20:38:20Z")

</div>

Still crashes the editor when I run it, even with the if(WidgetTree)  
link text

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2b03d27c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2b03d27c](https://forums.unrealengine.com/u/anonymous_user_2b03d27c)\
**Post date:** [November 17, 2016, 4:52pm UTC](https://forums.unrealengine.com/t/findwidget-crashes-unreal-engine/376225/5 "2016-11-17T16:52:26Z")

</div>

Where are you calling your Load function? Additionally, let me know if making your Load a UFUNCTION() has any affect on the behavior. When I mark the Load function (and other functions) as UFUNCTION() I’m able to call Load without any crash. Let me know if this helps or if you can provide steps to help me reproduce the issue locally.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f6022ca0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f6022ca0](https://forums.unrealengine.com/u/anonymous_user_f6022ca0)\
**Post date:** [November 18, 2016, 1:59am UTC](https://forums.unrealengine.com/t/findwidget-crashes-unreal-engine/376225/6 "2016-11-18T01:59:58Z")

</div>

I’m calling Load from a GameMode object in BeginPlay like so:

```
void ASpaceShooterGameMode::BeginPlay() {
	Super::BeginPlay();

	ChangeMenuWidget(StartingWidgetClass);

	((UGameWidget*)CurrentWidget)->Load();
}

```

ChangeMenuWidget sets CurrentWidget to the widget set in a edit anywhere UPROPERTY on the game mode. The GameMode used is a blueprint inheriting from this custom GameMode class.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2b03d27c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2b03d27c](https://forums.unrealengine.com/u/anonymous_user_2b03d27c)\
**Post date:** [November 21, 2016, 7:07pm UTC](https://forums.unrealengine.com/t/findwidget-crashes-unreal-engine/376225/7 "2016-11-21T19:07:19Z")

</div>

Hey Gochida-

I am still unable to reproduce the crash on my end and the provided log files do not indicate a crash occurring. If you’re able to reproduce the crash, could you provide a sample project that illustrates the issue. Additionally, you mentioned working through a tutorial, can you provide the tutorial link and where in the tutorial this occurred?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f6022ca0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f6022ca0](https://forums.unrealengine.com/u/anonymous_user_f6022ca0)\
**Post date:** [November 22, 2016, 6:01am UTC](https://forums.unrealengine.com/t/findwidget-crashes-unreal-engine/376225/8 "2016-11-22T06:01:45Z")

</div>

The tutorial is from a site called Zenva, specifically [Zenva Academy](https://academy.zenva.com) which sells programming courses. It was on sale for $9 so I figured I’d give it a shot. Unfortunately I don’t know of a way to link the course short of you purchasing it.

[I can link my project via google drive](https://drive.google.com/file/d/0B72mAd2e8P24N1d0bFUwdTB6R3M/view?usp=sharing), hope that works for you.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2b03d27c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2b03d27c](https://forums.unrealengine.com/u/anonymous_user_2b03d27c)\
**Post date:** [November 22, 2016, 2:23pm UTC](https://forums.unrealengine.com/t/findwidget-crashes-unreal-engine/376225/9 "2016-11-22T14:23:35Z")

</div>

The supplied project is missing the .uproject file and cannot be opened. Can you upload the project again?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f6022ca0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f6022ca0](https://forums.unrealengine.com/u/anonymous_user_f6022ca0)\
**Post date:** [November 22, 2016, 5:33pm UTC](https://forums.unrealengine.com/t/findwidget-crashes-unreal-engine/376225/10 "2016-11-22T17:33:49Z")

</div>

[Try this one maybe?](https://drive.google.com/open?id=0B72mAd2e8P24STYwa0g2MlBRRWs)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2b03d27c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2b03d27c](https://forums.unrealengine.com/u/anonymous_user_2b03d27c)\
**Post date:** [November 22, 2016, 8:23pm UTC](https://forums.unrealengine.com/t/findwidget-crashes-unreal-engine/376225/11 "2016-11-22T20:23:24Z")

</div>

Hey Gochida-

Thank you for providing the project again. This time I was able to open the project and investigate. I found that the issue was in fact a null pointer, the CurrentWidget variable was not set. If you make the pointer public and add `EditAnywhere, Instanced, BlueprintReadWrite, Category = Test` inside the UPROPERTY declaration, this will allow you to set any widget in your content browser to the pointer. After you’ve set the pointer in your game mode blueprint, you should be able to play without the crash occurring.

Cheers

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f6022ca0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f6022ca0](https://forums.unrealengine.com/u/anonymous_user_f6022ca0)\
**Post date:** [November 22, 2016, 11:15pm UTC](https://forums.unrealengine.com/t/findwidget-crashes-unreal-engine/376225/12 "2016-11-22T23:15:46Z")

</div>

I agree that this does work and should be the way to do it, though I think the original intent of the code was to have a Starting Widget that is first set to Current Widget, which is kind of redundant unless for some reason you needed to return to the starting widget.

That aside, it would be nice if the editor gave me an error report that a variable was null, rather than close out everything, shut the editor down and give me a “send crash report” window - that window is the whole reason I created a bug report in the first place!

Also makes trying to fix the null variable a bit of a pain. In the case of a even sneakier cause I can’t imagine how painful it would be to try and track it down and fix it. You aren’t even told what was null in the first place so it’s guesswork to narrow it down!
