# How can i assign static mesh to level like unreal editor with C++?

**URL:** <https://forums.unrealengine.com/t/how-can-i-assign-static-mesh-to-level-like-unreal-editor-with-c/362379>\
**Category:** Asset Creation\
**Tags:** unreal-engine, staticmesh, CPP, question\
**Created:** [June 29, 2016, 5:59am UTC](https://forums.unrealengine.com/t/how-can-i-assign-static-mesh-to-level-like-unreal-editor-with-c/362379 "2016-06-29T05:59:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_f5cbfceb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f5cbfceb](https://forums.unrealengine.com/u/anonymous_user_f5cbfceb)\
**Post date:** [June 29, 2016, 5:59am UTC](https://forums.unrealengine.com/t/how-can-i-assign-static-mesh-to-level-like-unreal-editor-with-c/362379/1 "2016-06-29T05:59:23Z")

</div>

Hello!

I want to assign static mesh to level like unreal editor !

So, How do I write the source code with C++ ?

Please answer about my question.

Thank you 😃

 ![96500-drag.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/7/7/077cb4a69fc05dd03e032b5626f939430d6bd771.png)

------------------------------------- ADD ---------------------------------------------------------------------

**Add ++ that class is UObject … So, I can’t (). always value is “NULL”.**

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4b3639a6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4b3639a6](https://forums.unrealengine.com/u/anonymous_user_4b3639a6)\
**Post date:** [June 29, 2016, 7:45am UTC](https://forums.unrealengine.com/t/how-can-i-assign-static-mesh-to-level-like-unreal-editor-with-c/362379/2 "2016-06-29T07:45:12Z")

</div>

Hey 4aries17  
You could check the SpawnActor Function from the UWorld Class.  
My Attempt would be:

```
UWorld* const World = ();
if (World)
{
    auto Actor = World->SpawnActor<AActorClass>(AActorClass::StaticClass());
    UStaticMeshComponent* ProjectileMesh = NewObject<UStaticMeshComponent>
                                            (Actor,
                                            UStaticMeshComponent::StaticClass());
    ProjectileMesh->RegisterComponent();
    Actor->RootComponent = ProjectileMesh;
    UStaticMesh* Projectile = //... Some way to load meshes in runtime....//;
    if(Projectile)
        ProjectileMesh->SetStaticMesh(Projectile);
}

```

I never tried this code, but thats the only way I could think of doing something like this 🙂  
If the code doesn’t compile I will look into this again.

Good Luck

Greetings

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f5cbfceb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f5cbfceb](https://forums.unrealengine.com/u/anonymous_user_f5cbfceb)\
**Post date:** [June 30, 2016, 1:51am UTC](https://forums.unrealengine.com/t/how-can-i-assign-static-mesh-to-level-like-unreal-editor-with-c/362379/3 "2016-06-30T01:51:10Z")

</div>

Thank you !  
I’ve tried . But I’m a Unreal novice.  
I can not access RootComponet.

 ![96610-12323.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/d/0/ed05e8b2d0ed1f47e93026f259b8fdc7541c189b.png)

 ![96609-121213.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/a/f/cafffc78da0f5d8407fddfc6ce453cc6641fcf72.png)

Do you know perhaps for this reason?

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [June 30, 2016, 2:17am UTC](https://forums.unrealengine.com/t/how-can-i-assign-static-mesh-to-level-like-unreal-editor-with-c/362379/4 "2016-06-30T02:17:08Z")

</div>

When you drop asset on level it spawn actors and UE4 have ready actor to contain those asset in level, usally just containg component that handles that asset

> **[ASkeletalMeshActor](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/Animation/ASkeletalMeshActor/)**
>
> SkeletalMeshActor is an instance of a \[USkeletalMesh\](API\\Runtime\\Engine\\Engine\\USkeletalMesh) in the world.

> **[AStaticMeshActor](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/Engine/AStaticMeshActor/)**
>
> StaticMeshActor is an instance of a \[UStaticMesh\](API\\Runtime\\Engine\\Engine\\UStaticMesh) in the world.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4b3639a6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4b3639a6](https://forums.unrealengine.com/u/anonymous_user_4b3639a6)\
**Post date:** [June 30, 2016, 8:17am UTC](https://forums.unrealengine.com/t/how-can-i-assign-static-mesh-to-level-like-unreal-editor-with-c/362379/5 "2016-06-30T08:17:15Z")

</div>

Hey 4Aries  
Sry I didn’t know that RootComponent is protected.  
Instead of using Actor-\>RootComponent you use Actor-\>SetRootComponent(ProjectileMesh);

Greetings

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4b3639a6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4b3639a6](https://forums.unrealengine.com/u/anonymous_user_4b3639a6)\
**Post date:** [June 30, 2016, 9:01am UTC](https://forums.unrealengine.com/t/how-can-i-assign-static-mesh-to-level-like-unreal-editor-with-c/362379/6 "2016-06-30T09:01:31Z")

</div>

Hey . Thats cool 🙂  
I didn’t know there were predefined Actors for this

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b08d4fb3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b08d4fb3](https://forums.unrealengine.com/u/anonymous_user_b08d4fb3)\
**Post date:** [June 30, 2016, 5:42pm UTC](https://forums.unrealengine.com/t/how-can-i-assign-static-mesh-to-level-like-unreal-editor-with-c/362379/7 "2016-06-30T17:42:13Z")

</div>

Hello,  
I wrote some code for you as example.

//Header

```
#pragma once

#include "Engine/StaticMeshActor.h"
#include "MyStaticMeshActor.generated.h"

UCLASS()
class *PUT_HERE_YOUR_PROJECT_API*_API AMyStaticMeshActor : public AStaticMeshActor
{
	GENERATED_BODY()
public:
	AMyStaticMeshActor();
};

```

// Source

```
#include "*PUT_HERE_YOUR_PROJECT_HEADER*.h"
#include "MyStaticMeshActor.h"

AMyStaticMeshActor::AMyStaticMeshActor()
{    
	UStaticMesh* StaticMeshTmp = LoadObject<UStaticMesh>(NULL, TEXT("/Engine/BasicShapes/Sphere.Sphere"));
	if(StaticMeshTmp != nullptr)
	{
		GetStaticMeshComponent()->Mobility = EComponentMobility::Movable;
		GetStaticMeshComponent()->SetStaticMesh(StaticMeshTmp);
	}
}

```

You can spawn this with AMyStaticMeshActor\* MyNewACtor = ()-\>SpawnActor\< AMyStaticMeshActor \>();  
Also it is possible to drag and drop from content browser.

I hope this was usefull for you.  
.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f5cbfceb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f5cbfceb](https://forums.unrealengine.com/u/anonymous_user_f5cbfceb)\
**Post date:** [July 1, 2016, 2:16am UTC](https://forums.unrealengine.com/t/how-can-i-assign-static-mesh-to-level-like-unreal-editor-with-c/362379/8 "2016-07-01T02:16:46Z")

</div>

Thank you very much!  
It’s very usefull to me!

But I don’t know where input a “AMyStaticMeshActor\* MyNewACtor = ()-\>SpawnActor\< AMyStaticMeshActor \>();” this code.

And How can i Get a StaticMeshComponent ?  
I don’t know that funtion logic “GetStaticMeshComponent()-\>”

Sorry to my code level …

4aries17.

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [July 1, 2016, 2:45am UTC](https://forums.unrealengine.com/t/how-can-i-assign-static-mesh-to-level-like-unreal-editor-with-c/362379/9 "2016-07-01T02:45:43Z")

</div>

Note that maybe except BSP, everything needs to be an actor to be in the world. What you see in Overlay is definitly a actor

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f5cbfceb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f5cbfceb](https://forums.unrealengine.com/u/anonymous_user_f5cbfceb)\
**Post date:** [July 1, 2016, 8:49am UTC](https://forums.unrealengine.com/t/how-can-i-assign-static-mesh-to-level-like-unreal-editor-with-c/362379/10 "2016-07-01T08:49:09Z")

</div>

Thank you,  
But that class is UObject … So I can’t (). return value is “NULL”… allways …

So, Is there a way to spawn in UObject class? or Is there a way to () in UObject class ?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b08d4fb3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b08d4fb3](https://forums.unrealengine.com/u/anonymous_user_b08d4fb3)\
**Post date:** [July 4, 2016, 5:02pm UTC](https://forums.unrealengine.com/t/how-can-i-assign-static-mesh-to-level-like-unreal-editor-with-c/362379/11 "2016-07-04T17:02:18Z")

</div>

I am glad to hear my first answer was usefull. Don’t forget to mark the acceptable answer, this help to see solution without full story for people from google.

I believe at begining of learning the UE4 YourGameMode::BeginPlay is good place for spawning something, most of time spawning actors is totally up to you.
