UE5 crashes while spawning an actor

I have three classes: PortalGun, PortalManager and Portal. PortalGun is using PortalManager to spawn Portals, but Unreal crashes after trying to spawn a Portal.

Crash Reporter:
UnrealEditor_PortalDemo_3259!UWorld::SpawnActor() [Source\Runtime\Engine\Classes\Engine\World.h:3474]
UnrealEditor_PortalDemo_3259!APortalManager::CreatePortalEnter() [Source\PortalDemo\PortalManager.cpp:39]
UnrealEditor_PortalDemo_3259!APortalGun::Shot() [Source\PortalDemo\PortalGun.cpp:60]
UnrealEditor_PortalDemo_3259!TBaseUObjectMethodDelegateInstance<0,APlayerCharacter,void __cdecl(void),FDefaultDelegateUserPolicy>::Execute() [Unreal Engine 5.2\UE_5.2\Engine\Source\Runtime\Core\Public\Delegates\DelegateInstancesImpl.h:535]

PortalGun.h
rider64_2hhnUZX8SV

PortalGun.cpp

PortalManager.h
rider64_5nDrLWUdat

PortalManager.cpp

Portal = GetWorld()->SpawnActor… this line is responsible for crashing the Unreal Engine. There might an issue in the way how the PortalManager is declared and/or initialized in the PortalGun/PortalGun().

you can post code directly to the forum (so you don’t need to make images)
"`"x3 the button typically next to 1 then on another line do it again

anything in between will be considered code.

on the problem at hand, I am going to presume that these portals will be spawned onto things that have collision like walls, and then the portal itself will also have collision when it spawns.
if this is the case then what is probably happening is the Physics system is throwing up its hands and giving up in a crash.
maybe because your APortal SceneComponent collider doesn’t have GenerateOverlapEvents == true
you might be able to fix this by feeding in the next argument of
UWorld::SpawnActor<T>(T::UObject, FVector, FRotator, FActorSpawnParameters)

besides looking at the collision response of the APortal objects collider

		FActorSpawnParameters SpawnParams;
		SpawnParams.bNoFail = true;
		SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;

additionally instead of feed in the UWorld* because any actor has access to GetWorld() (which you are doing anyways in this function) so you don’t need to pass it, maybe pass the FRotator of what would probably be the Normal Vector of the hit, and you could even pass a FHitResult& which would include the location of impact and the Normal Vector of the impact which you could then use to get the rotation.

your

const FRotator Rotation(0.0f, 0.0f, 0.0f);
// ^this is the same as
FRotator::ZeroRotator  // which is a static member variable of the FRotator class
1 Like

@gardian206 Thank you for your response. I will break my message into points.

  1. Code instead of images → will use that
  2. I have checked the collision of the Portal and it has none (imagine from UE5 BP_Portal)
    UnrealEditor_Y1KYCSwfRS
    I also added the SpawnParams to the spawning actor method as you suggested.
  3. That UWorld* is a thing from the past, I was testing between PortalManager inheriting from Actor and not. I removed it, and I added a reference to the HitResult, since it will be usefull to create Portal and the correct location with rotation, but I will focus on that one after I deal with the crash.
  4. ZeroRotator - good one, I used it instead my previous const Rotation.

After all UE5 is still crashing, actually there 2 reasons. When I fully regenerate the project (remove binaries and intermediate folder → use “Build.bat PortalDemo Win64 Development” in cmd → generate visual studio project files → build the project from VS) then I am able to boot the project and “play” the game. After I press left button (which activates the CreatePortalEnter() ) the UE5 crashes with the same crash report as above. After I try to boot the project again, UE5 crashes with another report:

"Crash Report - due to the "PortalManager = NewObject<APortalManager>();" line

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000970

UnrealEditor_TypedElementFramework
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_PortalDemo_0743!APortalGun::APortalGun() [M:\Programowanie\Unreal Engine\Projekty\PortalDemo\Source\PortalDemo\PortalGun.cpp:20]
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_Core
UnrealEditor_Core
UnrealEditor_Projects
UnrealEditor_Projects
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
kernel32
ntdll

your PortalGun construct APortalManager with NewObject
do not use NewObject<> on Actor
always use GetWorld()->SpawnActor

also, do not spawn actor at class constructor, do it at BeginPlay

1 Like

Okey @Hexavx, that helped. No more crashes so far, but Output Log shows one more warning and Portal is not spawned, after spawning APortal:
LogSpawn: Warning: SpawnActor failed because no class was specified

void APortalManager::CreatePortalEnter(const FHitResult& Hit)
{
	if(PortalEnter != nullptr)
		PortalEnter->Destroy();
	FActorSpawnParameters SpawnParams;
	SpawnParams.bNoFail = true;
	SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;

	PortalEnter = GetWorld()->SpawnActor<APortal>(PortalEnterClass, Hit.Location, FRotator::ZeroRotator, SpawnParams);
	UE_LOG(LogTemp, Warning, TEXT("Spawn done"));
}

it says, u haven’t set the PortalEnterClass

Yes, but I have, In Unreal Engine in a BP_PortalManager blueprint I set it :thinking:

PortalManager.h

private:
	UPROPERTY()
	APortal* PortalEnter;
	UPROPERTY()
	APortal* PortalExit;

	UPROPERTY(EditDefaultsOnly, Category="Portals")
	TSubclassOf<APortal> PortalEnterClass;

	UPROPERTY(EditDefaultsOnly, Category="Portals")
	TSubclassOf<APortal> PortalExitClass;
	
	void CreatePortal();

Maybe you spawn you cpp PortalManager, not the BP_PortalManager, which has the variable set?

1 Like

@ZaharKostic ou, you are right, I spawn PortalManager instead of BP_PortalManager. Do I need to add a class to PortalGun (class that has PortalManager pointer) and set it in UE5 to BP_PortalManager or there is some other way?

You can just forward declare it.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.