Hello. Im getting an Exception while trying to spawn some particles after I hit an object. This is the code:
header file:
UPROPERTY(EditDefaultsOnly, Category = "Explosion")
UParticleSystem* KilledEnemyParticle;
cpp file:
void ABulletProjectile::OnHit(UPrimitiveComponent * HitComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, FVector NormalImpulse, const FHitResult & Hit)
{
if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL))// && (OtherComp->IsSimulatingPhysics())))
{
OtherComp->AddImpulseAtLocation(GetVelocity() * 50.0f, GetActorLocation());
UGameplayStatics::SpawnEmitterAtLocation(this, BulletHitParticle, GetActorLocation());
if (OtherActor->ActorHasTag(TEXT("Destroyable")))
{
AActor* ActorToDestroy = OtherActor;
FTimerHandle TimerHandle;
FTimerDelegate TimerDelegate;
AZombieRatPawn* ZombieRat = Cast<AZombieRatPawn>(ActorToDestroy);
if (OtherActor != this)
{
if (ZombieRat != nullptr)
{
TimerDelegate.BindLambda([ActorToDestroy, this, ZombieRat]()
{
if (ZombieRat->CanEmmit)
{
UGameplayStatics::SpawnEmitterAtLocation(this, KilledEnemyParticle, ActorToDestroy->GetActorLocation());
ZombieRat->CanEmmit = false;
}
ActorToDestroy->Destroy();
});
GetWorld()->GetTimerManager().SetTimer(TimerHandle, TimerDelegate, 1.f, false);
}
}
}
}
Destroy();
}
Im getting this error after UE crashes:
Exception thrown at 0x0000000000000000 in UE4Editor.exe: 0xC0000005: Access violation executing location 0x0000000000000000. occurred
and this is the line that is failing:
UGameplayStatics::SpawnEmitterAtLocation(this, KilledEnemyParticle, ActorToDestroy->GetActorLocation());
any help would be appreciated. Thanks