Download

UE4 Crashed when im using AddComponent Function

Hi, i am trying to make changes and additions to the ready FPS package to improve myself.
When any cube is damaged i want it to create a Point Light within itself.
The only change i made is this;

//MyProjectProjectile.cpp
void AtrialProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
	// Only add impulse and destroy projectile if we hit a physics
	if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr) && OtherComp->IsSimulatingPhysics())
	{
		// OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());
		class UPointLightComponent* MyLight;
		OtherActor->AddComponent(FName("Light"), false, GetTransform(), MyLight);
	}
}

Thank you from now :blush:

MyLight is just a pointer, you’ll need to create an actual UPointLightcomponent and assign that pointer to it. As to how to correctly create a UPointLightComponent, i’m kind of busy in a meeting right now, but some googling returns something like… Right method to spawn component at runtime in 2019 - UE4 AnswerHub

2 Likes