Adding StaticMesh

Hi,

I am new to game development and Unreal Engine 4. I am using version 4.7

I have created a StaticMeshComponent and kept that as a RootComponent. Also I have added StaticMeshObject inside the code instead of adding it in the Details Panel. But its not working when I loaded it in the editor.

.h file:

UCLASS()
class BPCPP2_API AMyActor1 : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AMyActor1();

	virtual void PostInitializeComponents() OVERRIDE;

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	TSubobjectPtr<UStaticMeshComponent> StaticMeshComponent;

	UStaticMesh * Chair1;
	
};

.cpp file

AMyActor1::AMyActor1()

{

 	// 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;

	StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticComponent"));

	static ConstructorHelpers::FObjectFinder<UStaticMesh> ChairMesh1(TEXT("Content'/Chairs_Chair1.uasset'"));

	RootComponent = StaticMeshComponent;
	FVector Location(0, 0, 0);
	StaticMeshComponent->Mobility = EComponentMobility::Static;
	StaticMeshComponent->SetStaticMesh(Chair1);


}

Can somebody please check the code and help me to find the solution for this issue.

Thanks in advance!

Sorry I have changed that already. Can you check it?

AMyActor1::AMyActor1()

{

 	// 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;

	StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticComponent"));

	static ConstructorHelpers::FObjectFinder<UStaticMesh> ChairMesh1(TEXT("Content'/Chairs_Chair1.uasset'"));

	RootComponent = StaticMeshComponent;
	FVector offset;
	StaticMeshComponent->Mobility = EComponentMobility::Static;
	StaticMeshComponent->SetStaticMesh(ChairMesh1.Object);
	offset = FVector(0.0, 0.0, 0.0);
}

But still, I cannot see that Mesh is changing in the Details Panel.

What is Chair1 ? Your code declares the mesh variables as ChairMesh1, you should do

StaticMeshComponent->SetStaticMesh(ChairMesh1.Object);
//instead of
StaticMeshComponent->SetStaticMesh(Chair1);

Your path is probably not valid, to get the proper path copy it from the editor, by selecting the asset and doing Ctrl+C. It should look something like this :

"StaticMesh'/Game/Props/Cart/CartA.CartA'"

And btw, it’s pretty obvious from your 2 post that you do not understand what you are doing, for example
FVector offset;
offset = FVector(0.0, 0.0, 0.0);

does absolutely nothing. This is really basic stuff, you should do some tutorial on Unreal 4 or C++ programming in general.

Here is a good link : https://www.youtube.com/playlist?list=PLZlv_N0_O1gaCL2XjKluO7N2Pmmw9pvhE

No. Actually, I am selecting the object from the imported fbx file which I have kept it under content folder in Content Browser in the editor. I think its in /Game folder. I will try and let you know.

Actually I have changed that location as Gleb explained. I have already seen more tutorials for Blueprints but just I am entering into C++. Anyways thanks for sending the link. I will look for C++ tutorials.

Yah. But the link is likely to be like what michael Dube said. Just right click on your imported fbx and you’ll see some words like reference viewer or something like that. After you click that. Paste the link to your object finder( that would be the link to your imported fbx.

Thank you all so much for the help. I have tried with what you said and it shows the path “/Game/Chair1”. It worked.

Yup. Sorry i am not in front of ue right now, so i can’t check it by myself right now. But it would be like that. And if iam not forget. The idenyifier should automatically included (the StaticMesh). But your link shouldbe ok. (“StaticMesh’/Game/Chairs_Chair1”). Just try hehehe

Yes of course you are right. Sorry to mention that in above. Thank you so much.

You’re welcome. :slight_smile: jejeje