# Custom USceneComponent (or alternative)

**URL:** <https://forums.unrealengine.com/t/custom-uscenecomponent-or-alternative/414816>\
**Category:** C++\
**Tags:** scenecomponent, unreal-engine, cube, staticmesh, CPP, question\
**Created:** [January 17, 2018, 7:06pm UTC](https://forums.unrealengine.com/t/custom-uscenecomponent-or-alternative/414816 "2018-01-17T19:06:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Napoleonite](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Napoleonite](https://forums.unrealengine.com/u/Napoleonite)\
**Post date:** [January 17, 2018, 7:06pm UTC](https://forums.unrealengine.com/t/custom-uscenecomponent-or-alternative/414816/1 "2018-01-17T19:06:12Z")

</div>

I have a cube that derives from Actor and this cube has 6 walls. Now every wall is a StaticMeshComponent but I need to make it a class (or struct) instead so that I can later replace one of those walls with another class (like a class that has a double-door or something). But from what class do I derive?

- USceneComponent: Does not work: [https://answers.unrealengine.com/questions/743194/howto-add-staticmesh-to-scenecomponent.html](https://answers.unrealengine.com/questions/743194/howto-add-staticmesh-to-scenecomponent.html) Either an undocumented feature from UE4 or an UE4-bug. But this would have been the logical choice.

- AActor: I don’t think that it would be a good idea to make every wall an actor (so 7 actors per cube) for performance reasons and such.

- UActorComponent: Has no position in the world.

- UObject: This could in theory work but, I believe that it doesn’t automatically replicate and I can’t store any UStaticMeshComponents in it. It bugs UE4 out. Apparently when you have a UStaticMeshComponent placed in say Actor X and then store a reference to that component in another actor or UObject, UE4 will randomly crash and/or not render it. I think UE4 get’s confused between the pointer and the real object because they are the same in c++ code.

I tried everything I know, nothing seems to work.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ac2e3d15](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ac2e3d15](https://forums.unrealengine.com/u/anonymous_user_ac2e3d15)\
**Post date:** [January 17, 2018, 10:17pm UTC](https://forums.unrealengine.com/t/custom-uscenecomponent-or-alternative/414816/2 "2018-01-17T22:17:48Z")

</div>

You should derive from `USceneComponent` - I believe any other option is basically incorrect.

I read the question you linked and tested it for myself, and in a new project it seems to just work as I expected. Were you also seeing the same crash?

---

<div class="post-metadata">

**Author:** ![Napoleonite](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Napoleonite](https://forums.unrealengine.com/u/Napoleonite)\
**Post date:** [January 18, 2018, 6:23am UTC](https://forums.unrealengine.com/t/custom-uscenecomponent-or-alternative/414816/3 "2018-01-18T06:23:13Z")

</div>

Thanks I got it to work, my test code:

```
PrimaryComponentTick.bCanEverTick = false;

	this->SetMobility(EComponentMobility::Static);

	SMC = CreateDefaultSubobject<UStaticMeshComponent>(FName("PleaseDontCrash"));
	SMC->AttachToComponent(this, FAttachmentTransformRules::KeepRelativeTransform);
	SMC->SetMobility(EComponentMobility::Static);
	SMC->SetStaticMesh(ULib::CreateObject<UStaticMesh>(P_WALL_MESH));

```

But then I have no idea why it crashed before. But I’m not gonna complain, the code is and was good then. Thanks!

_P.S. Also make sure to add this line:_

```
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
```
