In unreal engine how to leave the texture2d in a material blank without leading to shader errors?

In unreal engine I want to set a texture2d to a material from the c++ end, however in the material’s graph editor I have to set the texture 2d to some initial texture2d otherwise it will complain about the shader error and at runtime setting the texture2d from the c++ end doesn’t work. If I instead set the texture2d slot of the material to some initial texture2d and then I can set the texture2d from c++ at runtime. I wonder if there is a way to leave the texture2d in the material blank until I set it at runtime in c++?

I have a slight issue understanding what you are trying to accomplish. If this is for the material editor / material creation, I am unsure if this will work. However if this is for a particular part of the code execution, simply using a IsValid equivalence could work.

Blueprint node IsValid is typically checking if a pointer is valid or not. In C++, pointers can be checked for validity by comparing them to nullptr (or NULL in older code):

if (MyPointer != nullptr)
{
    // The pointer is valid
}
else
{
    // The pointer is invalid
}

Alternatively, you can use the IsValid function provided by some Unreal Engine classes, such as UObject. This function checks if the object is valid and not pending destruction:

if (MyObject->IsValid())
{
    // The object is valid
}
else
{
    // The object is invalid
}

Note: not all classes provide the IsValid function, so you’ll need to refer to the specific class documentation or check if the function is available for the object you are working with.

Hi Anning5,

As EliasWick states, it’s always good practice to use “IsValid” tests in your code.
I think though, you’re refering to the fact the material has to have a “preview” texture otherwise you get a material graph error - that’s just how it is I’m afraid - I usually set them to the Engines default black texture (you may need to “show engine content” to see it)

I see, thank you guys!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.