Ошибка nullptr Unreal Engine 4
Получаю ошибку
Вызвано исключение: нарушение доступа для чтения. this->HealthComponent было nullptr.
STUBaseCharacter.h
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "STUBaseCharacter.generated.h"
class UCameraComponent;
class USTUHealthComponent;
class USpringArmComponent;
class UTextRenderComponent;
UCLASS()
class SHOOTTHEMUP_API ASTUBaseCharacter : public ACharacter
{
GENERATED_BODY()
public:
ASTUBaseCharacter(const FObjectInitializer &ObjInit);
protected:
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Components")
USTUHealthComponent *HealthComponent;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Components")
USpringArmComponent *SpringArmComponent;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Components")
UCameraComponent *CameraComponent;
STUBaseCharacter.cpp
#include "Player/STUBaseCharacter.h"
#include "Camera/CameraComponent.h"
#include "Components/InputComponent.h"
#include "Components/STUCharacterMovementComponent.h"
#include "Components/STUHealthComponent.h"
#include "Components/TextRenderComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
DEFINE_LOG_CATEGORY_STATIC(LogBaseCharacter, All, All);
ASTUBaseCharacter::ASTUBaseCharacter(const FObjectInitializer &ObjInit)
: Super(
ObjInit.SetDefaultSubobjectClass<USTUCharacterMovementComponent>(ACharacter::CharacterMovementComponentName))
{
PrimaryActorTick.bCanEverTick = true;
SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>("SpringArmComponent");
SpringArmComponent->SetupAttachment(GetRootComponent());
SpringArmComponent->bUsePawnControlRotation = true;
CameraComponent = CreateDefaultSubobject<UCameraComponent>("CameraComponent");
CameraComponent->SetupAttachment(SpringArmComponent);
HealthComponent = CreateDefaultSubobject<USTUHealthComponent>("HealthComponent");
HealthTextComponent = CreateDefaultSubobject<UTextRenderComponent>("HealthTextComponent");
HealthTextComponent->SetupAttachment(GetRootComponent());
}
void ASTUBaseCharacter::BeginPlay()
{
Super::BeginPlay();
check(HealthComponent); // ошибка
check(HealthTextComponent);
check(GetCharacterMovement());
OnHealthChanged(HealthComponent->GetHealth());
HealthComponent->OnDeath.AddUObject(this, &ASTUBaseCharacter::OnDeath);
HealthComponent->OnHealthChanged.AddUObject(this, &ASTUBaseCharacter::OnHealthChanged);
LandedDelegate.AddDynamic(this, &ASTUBaseCharacter::OnGroundLanded);
}
В чем может быть проблема? Если удаляю/комментирую - проект запускается
check(HealthComponent);
OnHealthChanged(HealthComponent->GetHealth());
HealthComponent->OnDeath.AddUObject(this, &ASTUBaseCharacter::OnDeath);
HealthComponent->OnHealthChanged.AddUObject(this, &ASTUBaseCharacter::OnHealthChanged);