I just starting to learn the Unreal Engine with C++. I made an actor and gave it a StaticMeshComponent and made it editable with UPROPERTY(). But when I deploy the actor in viewport the mesh component is not editable and gives the following message.
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TestSubject.generated.h"
UCLASS()
class PRACTICE1_API ATestSubject : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATestSubject();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(EditAnywhere)
UStaticMeshComponent* flyingObj;
};
TestSubject.cpp file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "TestSubject.h"
// Sets default values
ATestSubject::ATestSubject()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
flyingObj = CreateDefaultSubobject<UStaticMeshComponent>("StMesh");
}
// Called when the game starts or when spawned
void ATestSubject::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ATestSubject::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector Lift = GetActorLocation();
Lift.Z += 1.f;
SetActorLocation(Lift);
}
The main problem is there is no root component set. If you look in the output log window when you drop your BP in the viewport, you will see a warning similar to:
BP_TestSubject_C_0 has natively added scene component(s), but none of them were set as the actor's RootComponent - picking one arbitrarily
Sorry, that solution didn’t work the problem still persists…
I tried both of the options that you provided - either make flyingObj the root component or make a USceneComponent object and make it the root component - they both failed
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TestSubject.generated.h"
UCLASS()
class PRACTICE1_API ATestSubject : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATestSubject();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(EditAnywhere)
UStaticMeshComponent* flyingObj;
//USceneComponent* Root;
};
TestSubject.cpp file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "TestSubject.h"
// Sets default values
ATestSubject::ATestSubject()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
/*Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
SetRootComponent(Root);*/
flyingObj = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StMesh"));
SetRootComponent(flyingObj);
//flyingObj->SetupAttachment(Root);
}
// Called when the game starts or when spawned
void ATestSubject::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ATestSubject::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector Lift = GetActorLocation();
Lift.Z += 1.f;
SetActorLocation(Lift);
}