Download

Change texture image in Run Time

I’m doing this to learn C++ with Unreal Engine.
I just learned how to make a material instance and change a parameter value to change the color of an asset




UMaterialInstanceDynamic* MatInst = OtherComp->CreateAndSetMaterialInstanceDynamic(0);
        if (MatInst)
            {
                MatInst->SetVectorParameterValue("Color", FLinearColor::MakeRandomColor());
            }
 

… but I’m stuck in trying to see how to change a texture of an asset in Run Time, even if that involves creating a new instance, an enum, or anything else. What would be the right way to do this? I looked into CreateAndSetMaterialInstanceDynamicFromMaterial (int32 ElementIndx, UMaterialInterface *Parent), but I’m not sure how to pass the UMaterilInterface reference, or if this is the right way. Should I use an enum? Or perhaps load different material instances with different texture images?


void SetTextureParameterValue
(
[FName](https://api.unrealengine.com/INT/API/Runtime/Core/UObject/FName/index.html) ParameterName,
    class [UTexture](https://api.unrealengine.com/INT/API/Runtime/Engine/Engine/UTexture/index.html) * Value
)

https://api.unrealengine.com/INT/API…lue/index.html

It often helps to google the class name (UMaterialInstanceDynamic) to see what options are available. Luckily for you, it already does exactly what you need.

Now all you need to do is declare the texture in your header (.h) that you can assign in through the editor.



public:
UPROPERTY(EditAnywhere, Category = "Catz") [UTexture](https://api.unrealengine.com/INT/API/Runtime/Engine/Engine/UTexture/index.html)* CatTexture;


Also, metallic is 0 by default. However specular defaults at 0.5 and is how much ‘white light’ shines on your picture. You might want to set specular to 0 for a clearer cat pic.

Thank you for the workflow on how to assign this in the editor. However, my doubt is aimed towards finding an answer in how ti assign a different material in runtime. Maybe posting the picture with the cat contributed to the confusion.