How do I initialize values in override materials array (Tarray) in mesh component?

Hi. I have a generated mesh. In that mesh 1 override material is 1 mesh section. So to put material on it, I need same number of override materials as I have mesh sections. I can add materials inside the editor to that override materials like this: , but the problem is, that every mesh is different and has different number of mesh sections. So I need to create this dynamically from code.

I did try this:

_mesh->OverrideMaterials.Init(_baseMat, scene->mNumMeshes);

Which initialize the array to correct number of mesh sections, but it is initialized to null. And when I call

_dynMat = _mesh->CreateDynamicMaterialInstance(i);

I get a fatal error that index of array is invalid. It processes the 7 mats I added manualy from editor and then it stucks, cuz materials arent there anymore (just initialized values to null). So how do I assing the mat properly to use whole array?


If your array index is invalid, it’s because you are going out of range, iterate like this :

    for(int i =0; i < YourArray.Num(); i++)

Or you can check if yourArray[i] is valid.