C++ - array of mesh components, how to


How to correctly keep a couple of logically related mesh components in my class? (think sides of a dice that I want to be separate meshes)

(In the example below I use a UCustomMeshComponent which inherits from UMeshComponent)
It did work for me either by doing:

TSubobjectPtr<UCustomMeshComponent> SphereMesh1;
TSubobjectPtr<UCustomMeshComponent> SphereMesh2;


TArray<TSubobjectPtr<UCustomMeshComponent> > Sides;

However the latter one has some issues - for example cannot add an UPROPERTY() tag to it, as it crashes the engine (not being able to cast TArray to a property) and causes my custom meshes to disappear after I go to play mode and then exit from it.
The first one on the other hand is not very versatile…

Or maybe there is a better way to do it?

Thanks, great answer, that’s exactly what I needed.


if it is always 6 sides you can use a static array

TSubobjectPtr < UCustomMeshComponent > Sides[6];



struct FSubObjStruct
	TArray < TSubobjectPtr < UCustomMeshComponent > > Comps;
	int32 GetCount() const
		return Sides.Num();

//now you can use UPROPERTY()

FSubObjStruct Sides;


Sides.Comps.Add(new subobj);

Why not create the array variable in the derived blueprint and make a function in the c++ class that takes the array in from the blueprint and then call that function in the blueprint?

I’m attempting to do something similar, but my objects aren’t actually being exposed to the editor. When I try

UPaperSpriteComponent* DoorSprite[9];

…the first object is exposed, but none of the rest are. If I instead try to expose them individually, none of them are:

for (uint8 i = 0; i < _NumberOfDoors; i++){
     DoorSprite[i] = CreateDefaultSubobject<UPaperSpriteComponent>(*(FString::Printf(TEXT("DoorSprite_%u"), i)));
     DoorSprite[i]->AttachTo(Root, NAME_None, EAttachLocation::KeepWorldPosition);

were you ever able to find a solution? I am facing the same issue, can’t have a dynamic array of exposed meshes.

TSubobjectPtr is deprecated so this whole answer no longer works. What would be the correct way to do this now using CreateDefaultSubobject?