Download

Setting Material Instance Vector Parameter Value causes crash

Hi, I am trying to assign a dynamic material instance to a static mesh and then set a vector parameter value in the material with the following line of code but it crashes the editor every time. (this is in AMyActor BeginPlay)


MyDynamicMaterial->SetVectorParameterValue("Color", Green);

I have these in the header file:



        UPROPERTY()
	UMaterial* MyBaseMaterial;
	
	UPROPERTY()
	UMaterialInstanceDynamic* MyDynamicMaterial;

This in the constructor:


ConstructorHelpers::FObjectFinder<UMaterial> MaterialFinder(TEXT("Material'/Game/Materials/MyMaterial.MyMaterial'"));
	if (MaterialFinder.Object)
	{
		MyBaseMaterial = (UMaterial*)MaterialFinder.Object;
	}

And finally, this in Begin Play:


MyDynamicMaterial = (UMaterialInstanceDynamic*)MyBaseMaterial;
	MyMesh->SetMaterial(0, MyDynamicMaterial);
	
	if (MyDynamicMaterial)
	{
		MyDynamicMaterial->SetVectorParameterValue("Color", Green);
	}

“Green” is declared as an FLinearColor in the header file. The material instance gets applied to the mesh but I cannot change the vector parameter value. I get the following error code:


Access violation - code c0000005 (first/second chance not available)

UE4Editor_Engine!FMaterialRenderProxy::InvalidateUniformExpressionCache() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\engine\private\materials\materialshared.cpp:1744]
UE4Editor_Engine!FMaterialInstanceResource::RenderThread_UpdateParameter<FLinearColor>() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\engine\private\materials\materialinstancesupport.h:118]
UE4Editor_Engine!EURCMacro_SetMIParameterValue<FVectorParameterValue>::DoTask() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\engine\private\materials\materialinstance.cpp:322]
UE4Editor_Engine!TGraphTask<EURCMacro_SetMIParameterValue<FVectorParameterValue> >::ExecuteTask() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\core\public\async	askgraphinterfaces.h:886]
UE4Editor_Core!FNamedTaskThread::ProcessTasksNamedThread() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\core\private\async	askgraph.cpp:779]
UE4Editor_Core!FNamedTaskThread::ProcessTasksUntilQuit() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\core\private\async	askgraph.cpp:526]
UE4Editor_RenderCore!RenderingThreadMain() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\rendercore\private\renderingthread.cpp:310]
UE4Editor_RenderCore!FRenderingThread::Run() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\rendercore\private\renderingthread.cpp:417]
UE4Editor_Core!FRunnableThreadWin::Run() [d:\buildfarm\buildmachine_++ue4+release-4.11\engine\source\runtime\core\private\windows\windowsrunnablethread.cpp:74]

I am pretty new to this, any help would be really appreciated.
Thanks.

I tried to change this line: MyDynamicMaterial = (UMaterialInstanceDynamic*)MyBaseMaterial;

to this: MyDynamicMaterial = MyMesh->CreateAndSetMaterialInstanceDynamic(0);

Now the game won’t crash at the set vector parameter line, but now there is no material applied to my mesh at all when it spawns. Why is that? Can anyone clear this dynamic material creation up for me please, I am having a really hard time finding a solution from the available resources. Thanks.

It appears that you might not find the correct material on you find object. Also you should not upcast UXxxx class pointer with hard cast in C++ notation. Try to use the Cast<TargetType>(variable) notation. It will be returning nullptr if the variable is incompatible with the target class.

If you want to verify your material name really exists, create a Blueprint based of you C++ class and set the material and dynamic material properties in the editor. Than in the constructor - or better in the BeginPlay method as constructors sometimes are tricky if you the element is part of the current scene since it is called HotReloading for a reason - print the names of the material. It is easy to get a material name wrong if hard-coded.

Also if you use FindXXX methods you should always check if the object/class you find actually is valid (not null). If it is not present, fail the execution or before you access it verify if the pointer is not null before using it.

A reason why you did not find the material can be that the material is not part of the scene asset. Remember when building your scene (and the corresponding uasset file) will usually not contain anything that is not part of the scene (there is a special flag to include all the assets in your project).