I have implemented a mine in the toon tanks to try and practice and make the game my own. The mine works as intended when I drive over it, it will damage the tank, even destroy it.
The problem occurs when I shoot a projectile and it lands on the mine and crashes the engine. I assume this is a nullptr but probably wrong because I am doing the null checks I need.
The code my not be the best or the smartest way to do it, but I do want to destroy the mine if shot at, any help to point me in the right direction to solve this would be great.
LMine.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "LMine.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SphereComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Tank.h"
#include "HealthComponent.h"
// Sets default values
ALMine::ALMine()
{
PrimaryActorTick.bCanEverTick = false;
MineMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MineMesh"));
RootComponent = MineMesh;
CollisionSphere = CreateDefaultSubobject<USphereComponent>(TEXT("CollisionSphere"));
CollisionSphere->SetupAttachment(RootComponent);
CollisionSphere->InitSphereRadius(50.f);
CollisionSphere->OnComponentBeginOverlap.AddDynamic(this, &ALMine::OnOverlapBegin);
}
// Called when the game starts or when spawned
void ALMine::BeginPlay()
{
Super::BeginPlay();
}
void ALMine::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
if (OtherActor && OtherActor != this)
{
ATank* Tank = Cast<ATank>(OtherActor);
UHealthComponent* HealthComp = Cast<UHealthComponent>(Tank->GetComponentByClass(UHealthComponent::StaticClass()));
if (HealthComp)
{
HealthComp->DamageTaken(Tank, DamageAmount, nullptr, nullptr, this);
Destroy();
}
}
}
LMine.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SphereComponent.h"
#include "LMine.generated.h"
UCLASS()
class TOONTANKS_API ALMine : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ALMine();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* MineMesh;
UPROPERTY(VisibleAnywhere)
USphereComponent* CollisionSphere;
UPROPERTY(EditAnywhere)
float DamageAmount = 50.f;
UFUNCTION()
void OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
};
Error:
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000000
UnrealEditor_Engine
UnrealEditor_ToonTanks!ALMine::OnOverlapBegin() [E:\LaniganDEV\Projects\LittleTank\Source\ToonTanks\LMine.cpp:37]
UnrealEditor_ToonTanks!ALMine::execOnOverlapBegin() [E:\LaniganDEV\Projects\LittleTank\Intermediate\Build\Win64\UnrealEditor\Inc\ToonTanks\LMine.gen.cpp:34]
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Core
UnrealEditor_Core
UnrealEditor_Core
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_UnrealEd
UnrealEditor_UnrealEd
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
kernel32
ntdll