Download

Spawning UObject from TSubClassOf

As stated in the title

got that code


TSubclassOf<class UInventoryItem> DataObjectClass;

That i want to later use in BeginPlay


void AInventoryOnTheGround::BeginPlay()
{
	Super::BeginPlay();
	DataObjectInstance = DataObjectClass->GetDefaultObject<UInventoryItem>();
}


But starting the game crashes the engine with access violation 0x00000005 exeption

Why is that and is there any other way to create a UObject from TSubClassOf?

You haven’t given enough info to diagnose the crash. However, if you want to create a new instance, you shouldn’t be using GetDefaultObject. Try this instead:


DataObjectInstance = ConstructObject< UInventoryItem >(DataObjectClass);