# Can't save MaterialInstanceDynamic created via C++

**URL:** <https://forums.unrealengine.com/t/cant-save-materialinstancedynamic-created-via-c/451988>\
**Category:** Rendering\
**Tags:** reproducible-bug, unreal-engine, UE4, CPP, question\
**Created:** [July 9, 2019, 10:38pm UTC](https://forums.unrealengine.com/t/cant-save-materialinstancedynamic-created-via-c/451988 "2019-07-09T22:38:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4ea03db1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4ea03db1](https://forums.unrealengine.com/u/anonymous_user_a4ea03db1)\
**Post date:** [July 9, 2019, 10:38pm UTC](https://forums.unrealengine.com/t/cant-save-materialinstancedynamic-created-via-c/451988/1 "2019-07-09T22:38:23Z")

</div>

Can’t save D:/Documents/Programming/Unreal/DMI\_Bug/Content/MyMap.umap: Graph is linked to private object(s) in an external package.  
External Object(s):  
MaterialInstanceDynamic\_0

```
Try to find the chain of references to that object (may take some time)?

```

I’ve reproduced the bug in a standalone project and attached it here.  
To get the message, simply create an instance of MyActor (a C++ class) and then save.

link text

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4ea03db1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4ea03db1](https://forums.unrealengine.com/u/anonymous_user_a4ea03db1)\
**Post date:** [July 10, 2019, 3:13am UTC](https://forums.unrealengine.com/t/cant-save-materialinstancedynamic-created-via-c/451988/2 "2019-07-10T03:13:49Z")

</div>

unfortunately that doesn’t change anything. I had thought the same thing, is there a way of creating a material instance (instead of a material instance dynamic)

another thing to note is that if I make a dynamic material instance inside the blueprint construction script, everything works as I would expect it to

// the updated code that I tested with

```
AMyActor::AMyActor()
{
	static ConstructorHelpers::FObjectFinder<UMaterial> MyMaterial(TEXT("'/Game/MyMaterial'"));
	static ConstructorHelpers::FObjectFinder<UStaticMesh> MyMesh(TEXT("'/Game/MyMesh'"));

	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
	StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));
	StaticMeshComponent->SetupAttachment(RootComponent);
	if (MyMesh.Succeeded())
	{
		StaticMeshComponent->SetStaticMesh(MyMesh.Object);
	}
	if (MyMaterial.Succeeded())
	{
		MID_MyMaterial = UMaterialInstanceDynamic::Create(MyMaterial.Object, StaticMeshComponent);
	}
}

void AMyActor::PostInitializeComponents()
{
	Super::PostInitializeComponents();
	StaticMeshComponent->SetMaterial(0, MID_MyMaterial);
}

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4ea03db1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4ea03db1](https://forums.unrealengine.com/u/anonymous_user_a4ea03db1)\
**Post date:** [July 10, 2019, 3:37am UTC](https://forums.unrealengine.com/t/cant-save-materialinstancedynamic-created-via-c/451988/3 "2019-07-10T03:37:04Z")

</div>

quick update, it looks like by changing the OUTER of UMaterialInstanceDynamic::Create gets this to work  
changed from MeshComponent → NULL

edit:  
this is a bug, not a question, not resolved

---

<div class="post-metadata">

**Author:** ![Wattar](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wattar](https://forums.unrealengine.com/u/Wattar)\
**Post date:** [July 10, 2019, 4:04am UTC](https://forums.unrealengine.com/t/cant-save-materialinstancedynamic-created-via-c/451988/4 "2019-07-10T04:04:13Z")

</div>

I’m pretty sure it works and i just tested it again, maybe you had a corrupted blueprint? Deleting it and recreating it will probably work.  
As for the answer you posted, it works because you’re not giving the material instance any thing to be a part of, so it won’t save. it’s just like setting its flag to Transient.  
To test what i mean, set the outer to NULL like you said, and set the material in the costructor like you did before, without the PostInitializeComponents. And once you restart the editor the mesh will lose the material instance, because it didn’t get saved.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4ea03db1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4ea03db1](https://forums.unrealengine.com/u/anonymous_user_a4ea03db1)\
**Post date:** [July 11, 2019, 8:03pm UTC](https://forums.unrealengine.com/t/cant-save-materialinstancedynamic-created-via-c/451988/5 "2019-07-11T20:03:23Z")

</div>

I can make a blueprint instance of it (in the project), but if I drag and drop it into the world, it still won’t save

---

<div class="post-metadata">

**Author:** ![Wattar](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wattar](https://forums.unrealengine.com/u/Wattar)\
**Post date:** [July 11, 2019, 9:08pm UTC](https://forums.unrealengine.com/t/cant-save-materialinstancedynamic-created-via-c/451988/6 "2019-07-11T21:08:21Z")

</div>

Okay, after trying a couple of things i think this is the best way to go. As i said earlier, dynamic material instances are made to be created and edited during runtime so i suggest creating a constant material instance based on the material you wanna edit and using that in your constructor instead of the dynamic one, which will save just fine.

And if you want to edit it at runtime, just create a dynamic one based off of it and change its parametes, here’s the code i set up.

.h

![282216-5.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/1/5/f15857e9a13f0f1de805da30dce5df4e41e1169c.png)

.cpp

 ![282213-6.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/6/4/d6471db397bba5bd1e3b5e76c3e12fb1bb5a305d.png)

Here’s my in-editor setup with a material instance constant set to a red value, level and actor both save fine because the instance isn’t dynamic.

 ![282217-3.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/7/c/47ccdf03067f1f749744aa651cfc3f0fe2fae82e.png)

At runtime, a dynamic one is created and set to the mesh.

 ![282219-4.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/d/f/7dfdbf631204536cfeb2b12ccbf51a31a80fec01.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4ea03db1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4ea03db1](https://forums.unrealengine.com/u/anonymous_user_a4ea03db1)\
**Post date:** [July 11, 2019, 9:35pm UTC](https://forums.unrealengine.com/t/cant-save-materialinstancedynamic-created-via-c/451988/7 "2019-07-11T21:35:30Z")

</div>

thanks for the help dude, I’ll give it a go tonight when I get home

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4ea03db1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4ea03db1](https://forums.unrealengine.com/u/anonymous_user_a4ea03db1)\
**Post date:** [July 12, 2019, 3:35am UTC](https://forums.unrealengine.com/t/cant-save-materialinstancedynamic-created-via-c/451988/8 "2019-07-12T03:35:07Z")

</div>

alright yeah that does indeed work better, but it seems to have the same outcome as making a dynamic that has no outer. I guess there isn’t anyway around having to store the material settings (not the worst thing).

I might look into creating a material instance with the params I want and saving it to the content browser (probably not though)

thanks for all the help! I assume that material instance dynamic settings are never meant to be saved ?

---

<div class="post-metadata">

**Author:** ![Wattar](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wattar](https://forums.unrealengine.com/u/Wattar)\
**Post date:** [July 12, 2019, 3:39am UTC](https://forums.unrealengine.com/t/cant-save-materialinstancedynamic-created-via-c/451988/9 "2019-07-12T03:39:57Z")

</div>

No problem! You can save the instance settings some other way like you said but the dynamic instance still has to be created and edited during gameplay.
