Unreal engine keeps crashing when I hit play (no log)

I have this code:
Enemy.cpp


#include "Enemy.h"
#include "Components/StaticMeshComponent.h"
#include "Components/PrimitiveComponent.h"
#include "Components/BoxComponent.h"
#include "Engine/StaticMesh.h"
#include "Engine.h"

AEnemy::AEnemy()
{
    PrimaryActorTick.bCanEverTick = true;
}

void AEnemy::BeginPlay()
{
    Super::BeginPlay();

    FVector BoxSize = FVector(32.f, 32.f, 32.f);

    EnemyHitBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Enemy Hit Box"));
    EnemyHitBox->SetSimulatePhysics(true);
    EnemyHitBox->SetNotifyRigidBodyCollision(true);
    EnemyHitBox->OnComponentHit.AddDynamic(this, &AEnemy::OnHit);
    EnemyHitBox->SetBoxExtent(BoxSize);

    RootComponent = EnemyHitBox;

    StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Enemy Model"));
    StaticMesh->SetWorldScale3D(FVector(31.f, 31.f, 31.f));
    StaticMesh->SetupAttachment(RootComponent);
}
void AEnemy::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
}

void AEnemy::Damage(float Damage)
{
    Health -= Damage;

    if (Health <= 0.f) {
        Destroy();
    }
}
void AEnemy::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
    UE_LOG(LogTemp, Error, TEXT("It's a collision!"));
}


Enemy.h


#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Enemy.generated.h"

UCLASS()
class THECHOSENONE_API AEnemy : public AActor
{
    GENERATED_BODY()

public:    
    AEnemy();

protected:
    void BeginPlay() override;

public:
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    UBoxComponent* EnemyHitBox;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    UStaticMeshComponent* StaticMesh;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    float Health;

    void Tick(float DeltaTime) override;

    UFUNCTION(BlueprintCallable)
    void Damage(float Damage);

    UFUNCTION()
    void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit);

};

When I compile it, it works. When I play the game the engine just crashes. This is the wrong code, because it is the only code I have written.

Issue is you are doing stuff in the wrong place.



     FVector BoxSize = FVector(32.f, 32.f, 32.f);      
EnemyHitBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Enemy Hit Box"));    
EnemyHitBox->SetSimulatePhysics(true);    
EnemyHitBox->SetNotifyRigidBodyCollision(true);    
EnemyHitBox->OnComponentHit.AddDynamic(this, &AEnemy::OnHit);    
EnemyHitBox->SetBoxExtent(BoxSize);      
RootComponent = EnemyHitBox;      

StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Enemy Model"));    
StaticMesh->SetWorldScale3D(FVector(31.f, 31.f, 31.f));    
StaticMesh->SetupAttachment(RootComponent);


This should be in your constructor



 AEnemy::AEnemy() 

not in BeginPlay.

Also i would advise against binding a delegate in constructor,

this line



     EnemyHitBox->OnComponentHit.AddDynamic(this, &AEnemy::OnHit);  

should be in BeginPlay

so your code should look like this


#include "Enemy.h"
#include "Components/StaticMeshComponent.h"
#include "Components/PrimitiveComponent.h"
#include "Components/BoxComponent.h"
#include "Engine/StaticMesh.h"

AEnemy::AEnemy()
{
    PrimaryActorTick.bCanEverTick = true;

    EnemyHitBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Enemy Hit Box"));
    EnemyHitBox->SetSimulatePhysics(true);
    EnemyHitBox->SetNotifyRigidBodyCollision(true);
    EnemyHitBox->SetBoxExtent(FVector(32.f, 32.f, 32.f));

    RootComponent = EnemyHitBox;

    StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Enemy Model"));
    StaticMesh->SetWorldScale3D(FVector(31.f, 31.f, 31.f));
    StaticMesh->SetupAttachment(RootComponent);
}

void AEnemy::BeginPlay()
{
    Super::BeginPlay();

    EnemyHitBox->OnComponentHit.AddDynamic(this, &AEnemy::OnHit);

}
void AEnemy::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
}

void AEnemy::Damage(float Damage)
{
    Health -= Damage;

    if (Health <= 0.f) {
        Destroy();
    }
}
void AEnemy::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
    UE_LOG(LogTemp, Error, TEXT("It's a collision!"));
}

Yes thank you. But I ve got new problem. Can you help me to fix this too please? :slight_smile:

This can be a number of things, either your collision profile is not set to physics actor, or you need to open the mesh and assure use complex as simple is set. These are the most common issues.

Thank you, but I cant fix it.
Even this line of code:



 StaticMesh->SetWorldScale3D(FVector(31.f, 31.f, 31.f)); 

Is not working…
The cube is like 5644845664865456 bigger than the Hitbox which is



 FVector(32.f, 32.f, 32.f) 

And it still calls this error from previous message…
I will switch to unity, where I can get some help. Because here I will get nowhere…
You see, I dont see any error in my code… I am setting the size to be smaller than the box, and the cube is still almost 64x bigger than the small hitbox. Even when I resize it, it just deletes itself from the scene and becomes invisible…

Try: StaticMesh->SetWorldScale3D(FVector(0.9f, 0.9f, 0.9f)); It is scaling relative to the hitbox when you attach it hence it is so large. Or you can Attach and pass the flag to ignore scale.

the size remains same nomatter what for numbers I type there.

the StaticMesh->SetWorldScale3D is not working at all. even with that flag… it remains the same in size.

Good day!
Why is it better to addDynamic in BeginPlay not in constructor? because the object could not exist? Detailed explanation is much appreciated!

Because when i changed it from constructor to BeginPlay I’ve encountered this error::

https://i.imgur.com/0AP7WB5.png

Thank you!