# Attaching a StaticMeshComponent to A SceneComponent

**URL:** <https://forums.unrealengine.com/t/attaching-a-staticmeshcomponent-to-a-scenecomponent/361319>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [June 19, 2016, 3:48pm UTC](https://forums.unrealengine.com/t/attaching-a-staticmeshcomponent-to-a-scenecomponent/361319 "2016-06-19T15:48:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pulp\_user](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@pulp\_user](https://forums.unrealengine.com/u/pulp_user)\
**Post date:** [June 19, 2016, 3:48pm UTC](https://forums.unrealengine.com/t/attaching-a-staticmeshcomponent-to-a-scenecomponent/361319/1 "2016-06-19T15:48:57Z")

</div>

I am trying to to achieve the following hierarchy:

Pawn → StaticMeshComponent (Root Component) → SceneComponent A → StaticMeshComponent B

In other words: My pawn has a mesh-representation, and my scene component A also has one with B. I use the following code to achieve this in the SceneComponent A:

```
{
	GENERATED_BODY()

public:	
	UPROPERTY(EditAnywhere)
		bool bActivated = false;
	UPROPERTY(EditAnywhere)
		UStaticMeshComponent* mesh;
	// Sets default values for this component's properties
	UModule();

	// Called when the game starts
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;

		
	
};

```

Where UstaticMeshComponent\* mesh ist the pointer for B. I initialize it in the constructor:

```
UModule::UModule()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
	// off to improve performance if you don't need them.
	bWantsBeginPlay = true;
	PrimaryComponentTick.bCanEverTick = true;
	mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("mesh"));
	
	// ...
}

```

This is the result:

 ![95147-problem.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/7/5/67550626acd51eb45524dcd9e8f889baab5d7cee.png)

Both A and B are children of the RootComponent, not B child of A. I can edit the mesh of B through A (were B is accessible and editable), but not its material. When I select it I cant change its properties, because the engine says it is inhereted and must be declarde with Uproperty (which I did). It does not render in editor, but when I hit play it does. When I hover over it, it says “Introduced in: Unknown native source (via C++ code)” which makes absolutely no sense to me.

So I would like to know how to fix this and if my approach to giving both A and the pawn a mesh is right (I didnt find a way to include the mesh in the A itsself)

I have tried this in 4.10.4 and 4.12.3

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_93dbca8c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_93dbca8c](https://forums.unrealengine.com/u/anonymous_user_93dbca8c)\
**Post date:** [January 24, 2017, 1:57am UTC](https://forums.unrealengine.com/t/attaching-a-staticmeshcomponent-to-a-scenecomponent/361319/2 "2017-01-24T01:57:33Z")

</div>

`mesh->SetupAttachment(Module);`  
is what you are looking for.

---

<div class="post-metadata">

**Author:** ![pulp\_user](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@pulp\_user](https://forums.unrealengine.com/u/pulp_user)\
**Post date:** [January 24, 2017, 10:29am UTC](https://forums.unrealengine.com/t/attaching-a-staticmeshcomponent-to-a-scenecomponent/361319/3 "2017-01-24T10:29:30Z")

</div>

I should have closed this question a while ago since I found the answer, but yours is of course correct!  
One thing to add: `SetupAttachment(Module)` only works for components that have not yet been registered by `RegisterComponent()`. If it has been registered, one has to use `AttachToComponent(Module)`
