Unreal crashes every time you hit Play button (fixed)

A couple of videos back, I managed to get Unreal to crash every time I hit the Play button.

The first thing I thought was that setting a chair and table to be physics body types somehow caused the issue. So I restored these actors to be static and unchecked the collision checkboxes. Ok good. But, when I hit play again, Unreal still crashed.

No worries. Keep calm and try something else. What other changes did I make recently?

Then I realized the obvious: I had just participated in a code challenge. So maybe code that I wrote was causing the crash.

The new code I wrote was in TickComponent. So it ran every single frame.

So when clicking Play, the breaking code would run immediately, on the first frame, and continue as frames were being processed or “ticking.”

After commenting out all of the new code, and rebuilding my code through VS Build menu options, clicking Play no longer crashed Unreal. Yay! Now, I knew I could fix the issue.

After uncommenting the new code so it was processed again I narrowed the issue down to one statement:

	UE_LOG(LogTemp, Warning, TEXT("Collision with actor: %s"),
		*Hit->GetActor->GetName()
	);

Why was this crashing Unreal? …

It turns out “FHitResult Hit” did not have any data. So getting the name of the non-existing actor was the cause. GetWorld()->LineTraceSingleByObjectType wasn’t assigning anything to Hit because there was no actor collision from which to get a name on the very first frame of the game. So this was crashing unreal.

To solve I needed to wrap this UE_LOG statement in a condition:

AActor* ActorHit = Hit.GetActor();
if (ActorHit) { ...

As shown in the solution for this coding exercise.

CONCLUSION

Resetting my Unreal project to an earlier commit would have also done the trick. Resetting is a faster way to fix issues like this. Resetting is an alternative to investigating the specific cause of the crash. It has the benefit of speed. But, you are less likely to know exactly what caused the crash.

Learn from my mistake. If Unreal is crashing, it might be because you wrote some funky code.

In that case, it’s possible to remove or update code so it’s not funky. Then rebuild your project through VS build menu items. This is just another strategy you can take instead of resetting your unreal project to a previous version. Or, if your project has become so hopelessly funky, you can reset. It depends on how you prefer to solve the issue at the time.

3 Likes

Privacy & Terms