Search code examples
c++crashunreal-engine4unrealscript

Unreal Engine 5 crashing after using SetupAttachment function


I've created new poject and added new c++ class, but after using SetupAttachment UE has an error. I've tryed to fix it and found a probem. For now i don't know, in what problem, i actualy know the place. UE5 window after building

Code:

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;

};

And 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);

}

How to fix this errror? Is it mine mistake, or UE bug?


Solution

  • It’s your mistake. You’re never initializing SpringArm, and even if it were set in your actor instance (or through a blueprint or subclass), it wouldn’t be available during the constructor (and would still crash when constructing the CDO).