Issue: UStaticMeshComponent (Sword) becomes null / not valid the second time I hit play on the editor.
Video Showcase: https://youtu.be/N1LQXJMgVes
As you can see in the video. The first time I hit play, you are able to see the hit box of the Sword working just fine. When I stop the game and hit play again, for any reason the sword component becomes null on the C++ side but I can still see it on the viewport.
Here’s the Code where the UE Log message with the Error spans from:
void APlayerCharacter::HitDetect()
{
if (!IsValid(Sword))
{
UE_LOG(LogTemp, Error, TEXT("Invalid Sword reference"));
return;
}
UE_LOG(LogTemp, Error, TEXT("Breakpoint 1"));
if (HitObjectTypes.Num() <= 0)
{
UE_LOG(LogTemp, Warning, TEXT("No hit object types defined"));
return;
}
FVector HitStart = Sword->GetSocketLocation(TEXT("start"));
FVector HitEnd = Sword->GetSocketLocation(TEXT("end"));
FHitResult HitResult;
TArray<AActor*> ValidActorsToIgnore;
// Filter out invalid actors
for (AActor* Actor : ActorsToIgnore)
{
if (IsValid(Actor))
{
ValidActorsToIgnore.Add(Actor);
}
}
bool hit = UKismetSystemLibrary::SphereTraceSingleForObjects(
GetWorld(),
HitStart,
HitEnd,
10,
HitObjectTypes,
false,
ValidActorsToIgnore,
EDrawDebugTrace::ForDuration,
HitResult,
true
);
if (hit)
{
UE_LOG(LogTemp, Warning, TEXT("Just hit a pawn"));
}
}
For anyone wondering, if I do this:
if (Sword)
Instead of this:
if (!IsValid(Sword))
The engine crashes.
void APlayerCharacter::HitDetect()
Is Called from a Notify State script:
void UAnimNotifState_HItDetection::NotifyTick(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation,
float FrameDeltaTime, const FAnimNotifyEventReference& EventReference)
{
Super::NotifyTick(MeshComp, Animation, FrameDeltaTime, EventReference);
if (!IsValid(MeshComp) || !IsValid(MeshComp->GetOwner()))
{
return;
}
if (!PlayerCharacter)
{
PlayerCharacter = Cast<APlayerCharacter>(MeshComp->GetOwner());
}
if (PlayerCharacter)
{
UE_LOG(LogTemp, Warning, TEXT("Character is Valid for Notif State"));
PlayerCharacter->HitDetect();
}else
{
UE_LOG(LogTemp, Error, TEXT("Player Character Not Valid"));
}
}
What I’ve tried:
- Deleting, Intermediate, Saved, and Binaries folder, regenerating project files, and rebuild.
- Removing the category from uproperty for the Sword:
UPROPERTY(EditAnywhere)
UStaticMeshComponent* Sword;
I do not know what else todo at this point, I am just stuck. It makes no sense that it works the first time I hit play and not the second, nothing changes, the sword is clearly visible on screen the second time.
Here’s my PlayerCharacter constructor if that’s worth anything:
APlayerCharacter::APlayerCharacter()
{
PrimaryActorTick.bCanEverTick = true;
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Camera Spring Arm"));
SpringArm->SetupAttachment(RootComponent);
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Player Camera"));
Camera->SetupAttachment(SpringArm);
Sword = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SwordCompt"));
Sword->SetupAttachment(GetMesh() , TEXT("S_SwordR_v2"));
}