Invalid managed type found for the field `m_Materials` of the struct `Unity.Rendering.RenderMeshArray`.: the type `UnityEngine.Material[]` is a managed type and is not supported

I’m receiving the error:

Invalid managed type found for the field m_Materials of the struct Unity.Rendering.RenderMeshArray.: the type UnityEngine.Material[] is a managed type and is not supported

Invalid managed type found for the field `m_Materials` of the struct `Unity.Rendering.RenderMeshArray`.: the type `UnityEngine.Material[]` is a managed type and  is not supported
 at Unity.Entities.EntityManager.GetSharedComponentManaged(Unity.Entities.EntityManager* this, int sharedComponentIndex) (at E:\Blix\Blix2023\Library\PackageCache\com.unity.entities@1.0.0-pre.15\Unity.Entities\EntityManager.cs:1075)
 at Unity.Entities.ArchetypeChunk.GetSharedComponentManaged(Unity.Entities.ArchetypeChunk* this, Unity.Entities.SharedComponentTypeHandle`1<Unity.Rendering.RenderMeshArray> chunkSharedComponentData, Unity.Entities.EntityManager entityManager) (at E:\Blix\Blix2023\Library\PackageCache\com.unity.entities@1.0.0-pre.15\Unity.Entities\Iterators\ArchetypeChunkArray.cs:1087)
 at RenderChunk.Execute(RenderChunk* this, ref Unity.Entities.ArchetypeChunk chunk, int chunkIndexInQuery, bool useEnabledMask, ref Unity.Burst.Intrinsics.v128 chunkEnabledMask) (at E:\Blix\Blix2023\Temp\GeneratedCode\Assembly-CSharp\SystemRenderChunk__JobEntity_1215877010.g.cs:35)
 at RenderChunk.Unity.Entities.IJobChunk.Execute(RenderChunk* this, ref Unity.Entities.ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, ref Unity.Burst.Intrinsics.v128 chunkEnabledMask)
 at Unity.Entities.JobChunkExtensions.JobChunkProducer`1<RenderChunk>.ExecuteInternal(ref Unity.Entities.JobChunkExtensions.JobChunkWrapper`1<RenderChunk> jobWrapper, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex) (at E:\Blix\Blix2023\Library\PackageCache\com.unity.entities@1.0.0-pre.15\Unity.Entities\IJobChunk.cs:430)
 at Unity.Entities.JobChunkExtensions.JobChunkProducer`1<RenderChunk>.Execute(ref Unity.Entities.JobChunkExtensions.JobChunkWrapper`1<RenderChunk> jobWrapper, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex) (at E:\Blix\Blix2023\Library\PackageCache\com.unity.entities@1.0.0-pre.15\Unity.Entities\IJobChunk.cs:357)

When trying to apply the mesh(es) and material(s) to the RenderMeshArray.

    chnkMats = new Material[5]; //Material array for each submesh.
    for (int i = 0; i < chnkMats.Length; i++) {
          chnkMats = new Material(defaultMat);
          chnkMats.SetTexture("_Txtr2DArr", GetComponent<Server>().tileArrs);
          chnkMats.enableInstancing = true; 
    } 
    Mesh[] chunkMesh = new Mesh[1] { new Mesh {
        name = "Mesh " + I,
        indexFormat = IndexFormat.UInt32,
        subMeshCount = 5}
    };
    RenderMeshArray rMA = new RenderMeshArray(chnkMats, chunkMesh);

It only occurs when I try to build the game- which prevents me from completing the build. Though, I can run it in the editor. Is this an internal bug? I would love some help on this.

This is your callstack, but reduced to the minimum for clarity:

renderMeshArray.m_Materials is not supported because it is a managed type

at EntityManager.GetSharedComponentManaged

at ArchetypeChunk.GetSharedComponentManaged

at RenderChunk.Execute

at IJobChunk.Execute, or IJobEntity.Execute more likely

Thisveiled way of saying thatIJobEntityis not meant to be dealing with managed objects like thisMaterial`

To fix it: execute this particular code on the main thread outside job system.