SpawnActor doesn't work always with C++

I am figuring a problem out right now that some projectiles doesn’t spawn everytime they should.

.h

TSubclassOf<class AArrowTowerProjectile> ArrowTowerProjectileBP;
	AArrowTowerProjectile* Projectile = nullptr;

.cpp in constructor

	static ConstructorHelpers::FObjectFinder<UBlueprint> ProjectileBP (TEXT("Blueprint'/Game/Blueprints/Tower/BP_ArrowTowerProjectile.BP_ArrowTowerProjectile'"));
	ArrowTowerProjectileBP = (UClass*)ProjectileBP.Object->GeneratedClass;

and my tick function

void AArrowTower::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
	LastShotTime += DeltaTime;
	if (InRangeArray.Num() > 0)
	{
		if (LastShotTime > 1.f / ShotsPerSecond)
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::SanitizeFloat(LastShotTime));
			Projectile = World->SpawnActor<AArrowTowerProjectile>(ArrowTowerProjectileBP, ActorLocation, FRotator::ZeroRotator);
			Projectile->SetTarget(CalculateNearestNPC());
			Projectile->SetDamage(dmg);
			LastShotTime = 0.f;
		}
	}
}

I testet with a simple console output if the shooting part of the Tick-function is working properly but the projectiles are not spawning everytime. I cant see any reasonable behavior here.
Thanks for your help :slight_smile:

A default FActorSpawnParameters struct is passed as a default param inside SpawnActor. I’m guessing the projectile is sometimes failing the FindTeleportSpot check on line 297 LevelActor.cpp since SpawnActor checks collision when spawning by default. Check out your output window for log messages or alternatively debug when it returns null.

thanks for your answer, I fixed the problem by myself. The posted code ist alright, I just messed up a loop for calculating a target and sometimes that loop returned a nullptr, and so the projectile got destroyed instantly (which i set up too).
Problem Solved :slight_smile: