# Cast on BeginPlay() fails during runtime but otherwise succeeds

**URL:** <https://forums.unrealengine.com/t/cast-on-beginplay-fails-during-runtime-but-otherwise-succeeds/477249>\
**Category:** AI\
**Tags:** casting, beginplay, unreal-engine, question\
**Created:** [January 14, 2021, 11:22am UTC](https://forums.unrealengine.com/t/cast-on-beginplay-fails-during-runtime-but-otherwise-succeeds/477249 "2021-01-14T11:22:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bakeneko](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bakeneko](https://forums.unrealengine.com/u/Bakeneko)\
**Post date:** [January 14, 2021, 11:22am UTC](https://forums.unrealengine.com/t/cast-on-beginplay-fails-during-runtime-but-otherwise-succeeds/477249/1 "2021-01-14T11:22:56Z")

</div>

Hi guys,  
I’m initializing a reference to the pawns AIController on BeginPlay() inside the pawns class. This works just fine as long as I place the pawns before loading the game. The cast fails however, once I start spawning in pawns dynamically during runtime. Using the Gameplay AI Debugger shows that the pawns seemingly spawn with the correct controller and I can confirm that their Behaviour Tree is running. Unfortunately, my AI code does not fully execute this way since some of it’s functions are gated behind the valid reference to my AIController.

```
void AIMPEnemyNPC::BeginPlay()
{
	//...

	IMPController = Cast<AIMPEnemyNPC_AI>(GetController());
	if (IMPController)
	{
		UE_LOG(LogTemp, Warning, TEXT("%s"), *IMPController->GetName());
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Failed!"));
	}

    //....
}

```

I’m using the “Spawn AI From Class” Blueprint node to spawn the pawns. I have no idea why this Cast fails during runtime.  
Apparently I’m missing some background info about how the Engine works behind the scenes?

![327192-ue-log-output.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/9/a/29a26044db4adcbd95441cb4b1e9c89067ee46ad.jpeg)

---

<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:** [January 14, 2021, 4:43pm UTC](https://forums.unrealengine.com/t/cast-on-beginplay-fails-during-runtime-but-otherwise-succeeds/477249/2 "2021-01-14T16:43:42Z")

</div>

Hello! Take a look at this [https://answers.unrealengine.com/questions/1004334/spawn-actor-and-aicontroller-problem.html](https://answers.unrealengine.com/questions/1004334/spawn-actor-and-aicontroller-problem.html)

---

<div class="post-metadata">

**Author:** ![Bakeneko](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bakeneko](https://forums.unrealengine.com/u/Bakeneko)\
**Post date:** [January 15, 2021, 11:58am UTC](https://forums.unrealengine.com/t/cast-on-beginplay-fails-during-runtime-but-otherwise-succeeds/477249/3 "2021-01-15T11:58:57Z")

</div>

Thanks, Kehel18!  
Problem resolved.
