# An error when casting 

**URL:** <https://forums.unrealengine.com/t/an-error-when-casting/2501342>\
**Category:** Programming & Scripting\
**Tags:** UE-5, unreal-engine, cast, CPP, question\
**Created:** [May 10, 2025, 7:55pm UTC](https://forums.unrealengine.com/t/an-error-when-casting/2501342 "2025-05-10T19:55:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheSovietArmy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheSovietArmy](https://forums.unrealengine.com/u/TheSovietArmy)\
**Post date:** [May 10, 2025, 7:55pm UTC](https://forums.unrealengine.com/t/an-error-when-casting/2501342/1 "2025-05-10T19:55:39Z")

</div>

I was following this tutorial to help create some AI pathfinding using C++, and despite the tutorial being made in UE4 and my project using UE5.4, it has been the only tutorial I have found helpful for what I want to do so I decided to go with it. The tutorial was quite helpful until I reached a part of the video where I assume the person used a feature that is no longer available in UE5 and I am having trouble making it work.

```auto
void AAIEnemyController::OnMoveCompleted(FAIRequestID RequestID, const FPathFollowingResult& Result)
{
	AAICharacter* EnemyAI = Cast<AAICharacter>(GetControlledPawn());
	//Checks if cast was successful
	if (EnemyAI)
	{
		//Calls the function to pathfind
		EnemyAI->MoveToWaypoints();
	}
}

```

The error comes from the line GetControlledPawn, which seems not to exist in UE5 anymore.

I am very new at UE and C++ so any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![kaiagan1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kaiagan1/32/1056413_2.png) [@kaiagan1](https://forums.unrealengine.com/u/kaiagan1)\
**Post date:** [May 10, 2025, 9:32pm UTC](https://forums.unrealengine.com/t/an-error-when-casting/2501342/2 "2025-05-10T21:32:20Z")

</div>

Hey,

GetPawn() should be what you are looking for.

> **[GetPawn | Unreal Engine 5.5 Documentation | Epic Developer Community](https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/Engine/GameFramework/AController/GetPawn)**
>
> Overload list

---

<div class="post-metadata">

**Author:** ![TheSovietArmy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheSovietArmy](https://forums.unrealengine.com/u/TheSovietArmy)\
**Post date:** [May 10, 2025, 10:48pm UTC](https://forums.unrealengine.com/t/an-error-when-casting/2501342/3 "2025-05-10T22:48:37Z")

</div>

Thank you, that seems to have worked

---

<div class="post-metadata">

**Author:** ![kaiagan1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kaiagan1/32/1056413_2.png) [@kaiagan1](https://forums.unrealengine.com/u/kaiagan1)\
**Post date:** [May 11, 2025, 8:55am UTC](https://forums.unrealengine.com/t/an-error-when-casting/2501342/4 "2025-05-11T08:55:21Z")

</div>

I forgot to mention it, but posting it here for reference: there is a templated version of GetPawn, which lets you cast to your type directly. This is just for convenience, slightly faster to type:

Instead of:

```auto
AAICharacter* EnemyAI = Cast<AAICharacter>(GetPawn());

```

You can type:

```auto
AAICharacter* EnemyAI = GetPawn<AAICharacter>();

```

This doesn’t change anything, you still need to check if EnemyAI is valid, in case the cast failed.
