I have a C++ class, which calls CreateDefaultSubobject on its static mesh component in the constructor, but it crashes the editor!
I’ve read around this issue, but nothing seems to work.
Can anyone see anything wrong?
Header File:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "InteractiveObject.h"
#include "RSGenerator.generated.h"
UCLASS()
class RSGAME_API ARSGenerator : public AInteractiveObject
{
GENERATED_BODY()
UPROPERTY(VisibleDefaultsOnly, Category = Mesh)
class USkeletalMeshComponent* GenMesh;
public:
// Sets default values for this actor's properties
ARSGenerator();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
};
Source File:
// Fill out your copyright notice in the Description page of Project Settings.
#include "RSGame.h"
#include "RSGenerator.h"
// Sets default values
ARSGenerator::ARSGenerator()
{
GenMesh->CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
}
// Called when the game starts or when spawned
void ARSGenerator::BeginPlay()
{
Super::BeginPlay();
}
Whenever CreateDefaultSubobject function called editor crashes with following error.
Fatal error: [File:D:/Build/++UE4/Sync/Engine/Source/Runtime/CoreUObject/Private/UObject/Obj.cpp] [Line: 107] No object initializer found during construction.
If I move CreateDefaultSubobject and AttachToComponent functions to constructor of MyActor class, It works fine. Is there anyone know, why is this happening?
CreateDefaultSubobject can’t be used outside of the class constructor. As the name suggests, it’s for creating a default sub-object.
To create actor components at runtime you must use NewObject, then manually register it. Do NOT give the component a name, use the default NAME_None parameter.