Unreal Engine 5 使用 SetupAttachment 函数后崩溃

Unreal Engine 5 crashing after using SetupAttachment function

我已经创建了新项目并添加了新的 c++ class,但是在使用 SetupAttachment UE 后出现错误。我试图修复它并发现了一个问题。现在我不知道,在什么问题上,我实际上知道这个地方。 UE5 window after building

代码:

Header:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "TankController.generated.h"

class USpringArmComponent;
class UCameraComponent;

UCLASS()
class TANKS_API ATankController : public APawn
{
    GENERATED_BODY()

public:
    // Sets default values for this pawn's properties
    ATankController();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

    //DEFINTE COMPONENTS //

    // Do a Hull of the Tank
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Components")
    UStaticMeshComponent* Hull;

    // Wheels for the Tank
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Components")
    UStaticMeshComponent* Wheel1;
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Components")
    UStaticMeshComponent* Wheel2;

    //Tower of the Tank
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Components")
    UStaticMeshComponent* Turret;
  
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Components")
    UStaticMeshComponent* Barrel;

    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Components")
    UStaticMeshComponent* RecoilSystem;

    // Camera Components

    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Components")
    USpringArmComponent* SpringArm;
  
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Components")
    UCameraComponent* Camera;

public: 

    // Called to bind functionality to input
    virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

};

和 cpp:

// Fill out your copyright notice in the Description page of Project Settings.


#include "TankController.h"

#include "GameFramework/SpringArmComponent.h"

// Sets default values
ATankController::ATankController()
{
    // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = false;

    // Set Root Component to ours Hull
    RootComponent = Hull;

    // Attach Wheels to the Hull
    // Here i have an error
    SpringArm->SetupAttachment(Hull);

}

// Called when the game starts or when spawned
void ATankController::BeginPlay()
{
    Super::BeginPlay();
    
}

// Called to bind functionality to input
void ATankController::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

}

如何解决这个错误? 是我的错误,还是UE的bug?

这是你的错误。您永远不会初始化 SpringArm,即使它是在您的 actor 实例中设置的(或通过蓝图或子类),它在构造函数期间也不可用(并且在构造 CDO 时仍然会崩溃)。