Hello. I am just starting out with learning the C++ side of UE4 and I’m already running into some trouble.
I am just trying to create a basic C++ class for an object with a SceneComponent and a StaticMeshComponent. That is it.
The Scene component is created just fine, but calling SetStaticMesh for the other component causes the object to mysteriously delete itself so that BeginPlay is never reached. I receive no error messages. When the line is commented out, I can still set the mesh manually in the Details panel of the UE4 editor just fine. What am I doing wrong here?
Here is the constructor code for my pawn:
APlayerOne::APlayerOne()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//// Gather the assets.
// Locator for the player's mesh.
static ConstructorHelpers::FObjectFinder<UStaticMesh> P1MeshAssetLoc(
TEXT("StaticMesh'/Game/meshes/player/cube_1.cube_1'"));
if (P1MeshAssetLoc.Succeeded())
{
P1MeshAsset = P1MeshAssetLoc.Object;
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Error: Asset not found."));
}
//// Create the components.
// Create the scene component.
SceneComponent = CreateDefaultSubobject<USceneComponent>(
TEXT("SceneComponent"));
RootComponent = SceneComponent;
// Create the visual mesh component.
if (P1MeshAsset)
{
VisualMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(
TEXT("VisualMeshComponent"));
VisualMeshComponent->SetupAttachment(RootComponent);
VisualMeshComponent->SetMobility(EComponentMobility::Movable);
VisualMeshComponent->SetStaticMesh(P1MeshAsset);
VisualMeshComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
VisualMeshComponent->SetWorldScale3D(FVector(1.0f, 1.0f, 1.0f));
}
}