# Casting to spawned actor causes editor to crash

**URL:** <https://forums.unrealengine.com/t/casting-to-spawned-actor-causes-editor-to-crash/420815>\
**Category:** Blueprint\
**Tags:** casting, cast-failed, unreal-engine, crash, CPP, question, Blueprint\
**Created:** [March 25, 2018, 12:04pm UTC](https://forums.unrealengine.com/t/casting-to-spawned-actor-causes-editor-to-crash/420815 "2018-03-25T12:04:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_44db827a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_44db827a](https://forums.unrealengine.com/u/anonymous_user_44db827a)\
**Post date:** [March 25, 2018, 12:04pm UTC](https://forums.unrealengine.com/t/casting-to-spawned-actor-causes-editor-to-crash/420815/1 "2018-03-25T12:04:25Z")

</div>

I am trying to create a new instance of a class in my scene. I instantiate an actor based on a blueprint, then try to cast it to the corresponding class. When I compile the solution, my editor crashes. Anyone know why this could be?

```
	// Item that is used for empty slots in the inventory - makes it easier to switch between item slots and allows for more customisation by the designer
	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Inventory", Meta = (AllowPrivateAccess = "true"))
	TSubclassOf<class ACollectableObject> EmptySlotClass;
	ACollectableObject* EmptySlot = nullptr;

// In cpp...
// Spawn an empty slot actor into the world, so that we can reference it
	EmptySlot = Cast<ACollectableObject>(this->GetWorld()->SpawnActor(EmptySlotClass));

```

ACollectableObject is derived from AActor base class. Thank you in advance!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d590628f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d590628f](https://forums.unrealengine.com/u/anonymous_user_d590628f)\
**Post date:** [March 25, 2018, 12:28pm UTC](https://forums.unrealengine.com/t/casting-to-spawned-actor-causes-editor-to-crash/420815/2 "2018-03-25T12:28:47Z")

</div>

Maybe you’ve forgotten to assign EmptySlotClass a value in the blueprint. In any case, always try and remember to protect against null pointers,enclose your spawning code by a

```
if(EmptySlotClass){
EmptySlot = Cast<>();
} else { UE_LOG(LogTemp, Warning, TEXT("Mr Designer, please assign BP for the empty slot"))

```

You can checkout ensure / check macros as well, they are asserts and can prevent a crash and you can use them to logout messages to alert you of a nullptr…

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_44db827a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_44db827a](https://forums.unrealengine.com/u/anonymous_user_44db827a)\
**Post date:** [March 25, 2018, 12:57pm UTC](https://forums.unrealengine.com/t/casting-to-spawned-actor-causes-editor-to-crash/420815/3 "2018-03-25T12:57:08Z")

</div>

This stops the game from crashing! Thanks - however, even after I assign the blueprint in the editor, the empty slot class seems to get back a null reference and the cast does not happen. Any idea why this could be?

---

<div class="post-metadata">

**Author:** ![staticvoidlol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@staticvoidlol](https://forums.unrealengine.com/u/staticvoidlol)\
**Post date:** [March 25, 2018, 2:09pm UTC](https://forums.unrealengine.com/t/casting-to-spawned-actor-causes-editor-to-crash/420815/4 "2018-03-25T14:09:25Z")

</div>

Are you trying to use the Blueprint-assigned value in the constructor? If so, it won’t be available yet and you can rather use “OnConstruction”.
