SpawnActor isn't spawning anything in the world but it runs through code

I’m trying to get a shell to spawn out of my gun when i shoot it but for some reason nothing is spawning in the world. Here is my code

AActor* AFPS_Character::Eject_9mmShell()
{
	FActorSpawnParameters SpawnParams;
	SpawnParams.Instigator = this;
	const USkeletalMeshSocket* EjectSocket = GunRig->GetSocketByName("ShellEject");
	const FVector SocketTransform = EjectSocket->GetSocketLocation(GunRig);
	AActor* SpawnedShell = GetWorld()->SpawnActor<AShellEjection_9mm>(ShellToSpawn, SocketTransform, FRotator::ZeroRotator, SpawnParams);
	GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, "Debug");
	return SpawnedShell;
}

my cpp to spawn the actor through my character^

#include "ShellEjection_9mm.h"
#include "Components/SphereComponent.h"

// Sets default values
AShellEjection_9mm::AShellEjection_9mm()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	Shell = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Shell"));
	Shell->SetupAttachment(RootComponent);

	Sphere = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere"));
	Sphere->SetupAttachment(Shell);

}

// Called when the game starts or when spawned
void AShellEjection_9mm::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AShellEjection_9mm::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

the cpp of the actor I’m trying to spawn ^

here’s a screenshot of the bp actor
(it’s just a sphere for now since I’m just testing it)

I don’t get any errors and I even have a print statement underneath the SpawnActor and it prints the string, so everything runs fine

1 Like

try printing the actor location when it spawns, maybe the socket was wrong and it spawned a (0,0,0) for instance

Hmmm, I tried doing that and my unreal crashed actually. Did I write something wrong?

AActor* AFPS_Character::Eject_9mmShell()
{
	FActorSpawnParameters SpawnParams;
	SpawnParams.Instigator = this;
	const USkeletalMeshSocket* EjectSocket = GunRig->GetSocketByName("ShellEject");
	const FVector SocketTransform = EjectSocket->GetSocketLocation(GunRig);
	AActor* SpawnedShell = GetWorld()->SpawnActor<AShellEjection_9mm>(ShellToSpawn, SocketTransform, FRotator::ZeroRotator, SpawnParams);
	GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, SpawnedShell->GetActorLocation().ToString());
	return SpawnedShell;
}
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, SpawnedShell->GetActorLocation().ToString())

Here’s the print by itself ^

But I notice the actor doesn’t seem to be in the world at all when I run the function, from what I’m seeing in my level

image

Try adding:

    SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
1 Like

Hmmm, didn’t seem to do anything. Appreciate the suggestion though

Without knowing the scene: my guess is that it could be getting stuck inside a collision when spawning. With AlwaysSpawn it should at least stay in the outliner unless its set to destroy someplace else. No?

it could fall into the kill z zone but that wouldnt normally be instant


well here’s a video of my scene, I’d say its pretty open. I didn’t realize collisions could be a problem though so I tried reducing the collision size on the actor but it didn’t seem to do anything either

Is that when something falls of the map and it destroys the actor? I tried jumping off the map and it doesn’t do anything I just infinitely fall down if that’s what you mean

very strange, i thought it was on by default, whats the red debug line in your video, doesnt seem to be related to gunfire

I just forgot to turn the debug line off, I was gonna start fixing on getting gunshot decals working

I would suggest you try two things:

  1. Add a breakpoint at the spawn line to be sure its trying to spawn.
  2. Check when and possibly why its getting destroyed:
// .h
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;

// .cpp
void AShellEjection_9mm::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	Super::EndPlay(EndPlayReason);
	
	const FString s = FString::Printf(TEXT("EndPlayReason: %s"), *UEnum::GetDisplayValueAsText(EndPlayReason).ToString());
	GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, *s);
}

alright so i just decided to add a print underneath in the spawnactor class and the print doesn’t pop up. So the issue is probably related to the actor class, I just don’t know what could be causing the issue. Any ideas?

AShellEjection_9mm::AShellEjection_9mm()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	Shell = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Shell"));
	Shell->SetupAttachment(RootComponent);

	Sphere = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere"));
	Sphere->SetupAttachment(Shell);
	GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, "Debug");
}

just let me know if you need more screenshots of anything else

edit: I also just tried putting a print at the beginning of the function and nothing happens either

AShellEjection_9mm::AShellEjection_9mm()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, "Debug");

	Shell = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Shell"));
	Shell->SetupAttachment(RootComponent);

	Sphere = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere"));
	Sphere->SetupAttachment(Shell);
}

Show where Eject_9mmShell() is being executed.

Eject_9mmShell() is being executed in my player cpp file, I have all the code for my glock written in this one glock function

(sorry about all the random commented stuff out and the messy code, I’m super new to unreal if you haven’t realized yet lol)

void AFPS_Character::Glock17Func()
{
	//PlayAnimMontage(FireG17Anim, 1, NAME_None);

	FHitResult* HitResult = new FHitResult();
	FVector StartTrace{ Camera->GetComponentLocation() };
	FVector EndTrace{ StartTrace + (GetViewRotation().Vector() * 2000.f) };
	FCollisionQueryParams* TraceParams = new FCollisionQueryParams();
	if (bHasGlock17 && bHoldingGlock17)
	{
		//GetWorld()->LineTraceSingleByChannel(*HitResult, StartTrace, EndTrace, ECC_Visibility, *TraceParams)
		if (GetWorld())
		{
			ATestDummy* TestTarget = Cast<ATestDummy>(HitResult->GetActor());
			DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor::Red, false, .5f);

			UAnimInstance* AnimInstanceArm = ArmRig->GetAnimInstance();
			UAnimInstance* AnimInstanceGun = GunRig->GetAnimInstance();


			//&& FireG17GunAnim
			if (AnimInstanceGun)
			{
				AnimInstanceArm->Montage_Play(FireG17ArmsAnim);
				//AnimInstance->Montage_JumpToSection("Recall");
				AnimInstanceGun->Montage_Play(FireG17GunAnim);
				GetWorld()->GetFirstPlayerController()->PlayerCameraManager->StartCameraShake(CamShake);
				Eject_9mmShell();
				
				G17_GunshotSound->Play();

			//	AnimInstance->Montage_Play(FireG17GunAnim);
			//	AnimInstance->Montage_JumpToSection("Recall");

				const USkeletalMeshSocket* GunBarrelSocket = GunRig->GetSocketByName("MuzzleFlashSocket");

				if (GunBarrelSocket)
				{
					//GunBarrelSocket->GetSocketTransform(GetMesh());
					const FVector SocketTransform = GunBarrelSocket->GetSocketLocation(GunRig);
					if (MuzzleFlash)
					{
						//UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), MuzzleFlash, SocketTransform);
						
						//UNiagaraFunctionLibrary::SpawnSystemAtLocation(GetWorld(), MuzzleFlash, SocketTransform);


						

					}
				}
			}
			

			if (TestTarget)
			{
				TestTarget->DamageTarget(50.f);
			}
		}
	}
}

under “If (AnimInstanceGun)”

Move it above if (AnimInstanceGun) to get it working.

I’m on phone right now… the format is a bit all over the place to see all the code.

Nothing changed, still runs the Eject_9mmShell() function and shows the print on screen but its not showing the print from the spawnactor

And there is no "SpawnActor failed… " message in the Output Log and the EndPlay(…) override prints nothing?

lets look at the shell too, is it destroying itself, for instance is its lifespan set too low as you would want this set for a shell so it autodeletes

:point_up_2: If it gets destroyed instantly this should tell us. If it fails to spawn the Output Log will say. The only other way I can think to help is @Lime_Energy shares an empty project with the character, weapon and shell classes.