# Add Component from another component

**URL:** <https://forums.unrealengine.com/t/add-component-from-another-component/386991>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [March 5, 2017, 11:10am UTC](https://forums.unrealengine.com/t/add-component-from-another-component/386991 "2017-03-05T11:10:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_4b2e97c7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4b2e97c7](https://forums.unrealengine.com/u/anonymous_user_4b2e97c7)\
**Post date:** [March 5, 2017, 11:10am UTC](https://forums.unrealengine.com/t/add-component-from-another-component/386991/1 "2017-03-05T11:10:06Z")

</div>

Is where any way to add static mesh component to actor from another component (like that

![128740-screensave.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/a/2/aa2c1bd5230269a56bfcc4dd22a4ddc811c8ff5b.png)

)?

As I understend it is imposible in blueprint, but maby it is poseble in c++ code?  
I tried to do that with

```
SMC = NewObject<UStaticMeshComponent>(GetOwner());
SMC->RegisterComponent(); 

```

and that

```
SMC = GetOwner()->CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SMC"));

```

But it crashed every time… Any idea?

P.S. .h

```
UStaticMeshComponent* SMC;

```

---

<div class="post-metadata">

**Author:** ![Wurmloch83](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wurmloch83](https://forums.unrealengine.com/u/Wurmloch83)\
**Post date:** [March 5, 2017, 1:24pm UTC](https://forums.unrealengine.com/t/add-component-from-another-component/386991/2 "2017-03-05T13:24:27Z")

</div>

Why can’t you just connect `GetParent` with the `Target` pin in blueprint? Is the parent not valid in the component from where you try to add the other to the parent?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4b2e97c7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4b2e97c7](https://forums.unrealengine.com/u/anonymous_user_4b2e97c7)\
**Post date:** [March 5, 2017, 3:37pm UTC](https://forums.unrealengine.com/t/add-component-from-another-component/386991/3 "2017-03-05T15:37:12Z")

</div>

First of all, parrant is valid but function “Add static mesh component” (or any other component) is not.

 ![128760-screen.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/0/7/207d16610df3054bcdba115c155570809ca020f7.png)

Secondly, I don’t wont use blueprint here to much, because I would need to add components once at a 0.01 second or less, and blueprint cant do this. Blueprints do function once at least at one frame, and one frame lasts 0.017-0.033 s (60fps-30fps).

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4b2e97c7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4b2e97c7](https://forums.unrealengine.com/u/anonymous_user_4b2e97c7)\
**Post date:** [March 5, 2017, 3:41pm UTC](https://forums.unrealengine.com/t/add-component-from-another-component/386991/4 "2017-03-05T15:41:20Z")

</div>

Also I tried just copy paste function “Add static mesh component”, but it is not work either.

---

<div class="post-metadata">

**Author:** ![Wurmloch83](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wurmloch83](https://forums.unrealengine.com/u/Wurmloch83)\
**Post date:** [March 5, 2017, 4:07pm UTC](https://forums.unrealengine.com/t/add-component-from-another-component/386991/5 "2017-03-05T16:07:39Z")

</div>

**For Blueprint** : What about calling a custom function on the actor? The custom function can then add the static mesh.

**For C++** : have you tried starting the editor from within visual studio, the debugger shall then show where the crash happens.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4b2e97c7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4b2e97c7](https://forums.unrealengine.com/u/anonymous_user_4b2e97c7)\
**Post date:** [March 5, 2017, 4:35pm UTC](https://forums.unrealengine.com/t/add-component-from-another-component/386991/6 "2017-03-05T16:35:12Z")

</div>

Fatal error: [File:D:\Build++UE4+Release-4.13+Compile\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\Obj.cpp] [Line: 84]  
No object initializer found during construction.

Here crash log, but I don’t clearly understend how to give him object initializer in code like that:

```
GetOwner()->CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshMeshMesh"));

```

---

<div class="post-metadata">

**Author:** ![Wurmloch83](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wurmloch83](https://forums.unrealengine.com/u/Wurmloch83)\
**Post date:** [March 5, 2017, 5:16pm UTC](https://forums.unrealengine.com/t/add-component-from-another-component/386991/7 "2017-03-05T17:16:33Z")

</div>

`ConstructSubobject` can only be called from constructor, from elsewhere you need to call `NewObject`. You said you already tried this, but where is it chrashing then?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4b2e97c7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4b2e97c7](https://forums.unrealengine.com/u/anonymous_user_4b2e97c7)\
**Post date:** [March 5, 2017, 5:45pm UTC](https://forums.unrealengine.com/t/add-component-from-another-component/386991/8 "2017-03-05T17:45:39Z")

</div>

I tried do this, but static mesh not appears.

```
LastSMC = NewObject<UStaticMeshComponent>(GetOwner());
		LastSMC->StaticMesh = TestSMCReference->StaticMesh;
		LastSMC->SetWorldTransform(TestSMCReference->GetComponentTransform());

```

But ok, it is not crashed. (I don’t know why it crashed first time)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4b2e97c7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4b2e97c7](https://forums.unrealengine.com/u/anonymous_user_4b2e97c7)\
**Post date:** [March 5, 2017, 7:16pm UTC](https://forums.unrealengine.com/t/add-component-from-another-component/386991/9 "2017-03-05T19:16:27Z")

</div>

Ok, I checked all code second time, corrected the most stupid bugs, and it’s work now. But another problem appear. Static Mesh Components invisible, and I don’t know why. I can teleported to this component, but they are invisible…

---

<div class="post-metadata">

**Author:** ![intoxicat3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@intoxicat3](https://forums.unrealengine.com/u/intoxicat3)\
**Post date:** [September 3, 2018, 3:15pm UTC](https://forums.unrealengine.com/t/add-component-from-another-component/386991/10 "2018-09-03T15:15:25Z")

</div>

Trying to add component from component using Blueprints and I can’t 🙂 I don’t want to create hacks and call something on Actor just because of that… I’m teaching Blueprints and it’s another thing that is missing, need to do it in C++.

---

<div class="post-metadata">

**Author:** ![dZh0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dZh0](https://forums.unrealengine.com/u/dZh0)\
**Post date:** [September 3, 2018, 7:58pm UTC](https://forums.unrealengine.com/t/add-component-from-another-component/386991/11 "2018-09-03T19:58:54Z")

</div>

You should set the static mesh in the component like this:

 ![252604-setstaticmesh.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/0/5/e0502f81cdb2c50dea9237b279103659ec8622b9.png)

But first you’ll have to add a static mesh component to the Actor:

![252605-myactor.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/3/7/137b014ac8377ea35d1305b7e342069ac15f72c0.png)

This is the same as declaring `UStaticMesh*` pointer in C++.

As in Blueprint, you _CAN NOT_ add member variables in C++ run-time. All class members must be defined during compile-time.

---

<div class="post-metadata">

**Author:** ![dZh0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dZh0](https://forums.unrealengine.com/u/dZh0)\
**Post date:** [September 3, 2018, 8:29pm UTC](https://forums.unrealengine.com/t/add-component-from-another-component/386991/12 "2018-09-03T20:29:48Z")

</div>

That being said, you can spawn a new actor with a static mesh and attach it it to the parent actor but this is not advisable in large scale.

 ![252611-addactor.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/b/3/cb32e15c2be6fd08df49c4f76aed2c018da49056.png)
