# Instanced Static Mesh

**URL:** <https://forums.unrealengine.com/t/instanced-static-mesh/358872>\
**Category:** Asset Creation\
**Tags:** newobject, unreal-engine, Instanced-Mesh, CPP, question\
**Created:** [May 28, 2016, 2:16pm UTC](https://forums.unrealengine.com/t/instanced-static-mesh/358872 "2016-05-28T14:16:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_23c91a471](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_23c91a471](https://forums.unrealengine.com/u/anonymous_user_23c91a471)\
**Post date:** [May 28, 2016, 2:16pm UTC](https://forums.unrealengine.com/t/instanced-static-mesh/358872/1 "2016-05-28T14:16:50Z")

</div>

Hi all I’m playing around with instanced static meshes and now when I initialize and create everything in constructor and create my instanced static mesh with call to “CreateDefaultSubobject\<\>” everything works as intended the instance is created and I can add instances of static meshes, I would like to move this code away from constructor and execute it in BeginPlay() method however I encountered a problem the “CreateDefaultSubobject\<\>” can be called only in constructor so I’m using “NewObject\<\>” to create InstancedStaticMeshComponent the instance is created successfully I can set a static mesh on it, however when I call “AddInstance” passing in transform the exception is thrown I’ve investigated a bit and it seams that it is thrown because the OwnerActor is nullptr here a code

Code From Constructor this works fine

```
instancedMeshesComponent = CreateDefaultSubobject<UMyInstancedStaticMeshComponent>(*tempName);
instancedMeshesComponent->AttachTo(RootComponent);

instancedMeshesComponent->SetStaticMesh(MeshType); // MeshType is set to cube

FTransform transform;
transform.SetLocation(FVector(0, 0, 0));

instancedMeshesComponent->AddInstance(transform);

```

Code from BeginPlay this doesnt work the exception is thrown at “AddInstance(transform)” line also I’m not sure what I should pass in as the “Outer” argument so I just pass in current level but I’ve tried to pass in “World” and also the StaticClass() of the object where instancedStaticMesh is declared no changes

```
    instancedMeshesComponent = NewObject<UMyInstancedStaticMeshComponent>(GetWorld()->GetCurrentLevel(),*tempName);
    instancedMeshesComponent->AttachTo(RootComponent);
    
    instancedMeshesComponent->SetStaticMesh(MeshType); // MeshType is set to cube
    
    FTransform transform;
    transform.SetLocation(FVector(0, 0, 0));
    
    instancedMeshesComponent->AddInstance(transform); // exception is thrown at this line

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_23c91a471](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_23c91a471](https://forums.unrealengine.com/u/anonymous_user_23c91a471)\
**Post date:** [May 28, 2016, 4:44pm UTC](https://forums.unrealengine.com/t/instanced-static-mesh/358872/2 "2016-05-28T16:44:53Z")

</div>

Right passing in “this” pointer as Outer argument solved a half of the problem now it doesn’t throw exception but the mesh is not rendered

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_6b6e43611](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_6b6e43611](https://forums.unrealengine.com/u/anonymous_user_6b6e43611)\
**Post date:** [January 14, 2019, 7:52pm UTC](https://forums.unrealengine.com/t/instanced-static-mesh/358872/3 "2019-01-14T19:52:43Z")

</div>

I was having the same issue. Looks like you need to add

```
instancedMeshesComponent->RegisterComponentWithWorld(GetWorld());

```

to get it to appear
