[Resolved] Native Array of struct containing different types. Error InvalidOperationException

I am just starting to work with Job System and Burst Compiler(1.4.0) and I have a problem.

I have a structure with arrays of different types. When I try to create a NativeArray out of it, I get an error on startup “InvalidOperationException: ‘Data’ used in NativeArray<> must be unmanaged (contain no managed types) and cannot itself be a native container type.” I don’t know what to do, I tried to use native arrays instead of unity arrays, but I still get the same error. The structure is used in another class to return the required parameters

Class:

public class Model
    {
        public List<GameObject> gameObjects;
        public Vector3[] vertices;
        public int[] triangles;
        public Vector2[] uvs;
        public Vector3[] normals;

        public Data GetData()
        {
            return new Data(vertices, triangles, uvs, normals, gameObjects.Count);
        }
    }

Structure:

public struct Data
    {
        public readonly int gameObjectsCount;
        public readonly Vector3[] vertices;
        public readonly int[] triangles;
        public readonly Vector2[] uvs;
        public readonly Vector3[] normals;

        public Data(Vector3[] _vertices, int[] _triangles, Vector2[] _uvs, Vector3[] _normals, int _gameObjectsCount)
        {
            vertices = _vertices;
            triangles = _triangles;
            uvs = _uvs;
            normals = _normals;
            gameObjectsCount = _gameObjectsCount;
        }
    }

The line that cause the error:

int length = 10;
NativeArray<Data> _data = new NativeArray<Data>(length, Allocator.Persistent); 

Does anyone have an idea of how to get around this? Thanks in advance.

Caveat: I’m only just starting out with the Jobs system, but…
My take on this is that you are passing managed arrays (non-blittable types) into the structure (by reference) which could be manipulated outside the job effectively circumventing the thread safety checks the job system tries to maintain to protect against race conditions. Your job could be reading the vertices while your class is changing their values. The readonly qualifier only means the array reference can’t be changed, not the properties or contents of it. My guess is that you need to set up NativeArray versions and copy the data into the structure. If you aren’t finding this works, there is probably something different going on that still triggers the error.

I gave up on the idea of using NativeArray, instead I used NativeStream. Maybe it will be useful for someone

private void AddToStream(int index, Data data)
    {
        writer.BeginForEachIndex(index);

        writer.Write(data.elementsCount);

        writer.Write(data.vertices.Length);
        foreach (float3 vertex in Extestions.Vector3ToFloat3(data.vertices))
        {
            writer.Write(vertex);
        }

        writer.Write(data.triangles.Length);
        foreach (int triangle in data.triangles)
        {
            writer.Write(triangle);
        }

        writer.Write(data.uvs.Length);
        foreach (float2 vertex in Extestions.Vector2ToFloat2(data.uvs))
        {
            writer.Write(vertex);
        }

        writer.Write(data.normals.Length);
        foreach (float3 normal in Extestions.Vector3ToFloat3(data.normals))
        {
            writer.Write(normal);
        }

        writer.EndForEachIndex();
    }
    public Data GetLookUpFromStream(int index)
    {
        NativeStream.Reader reader = stream.AsReader();

        reader.BeginForEachIndex(index);

        var vertices = new List<Vector3>();
        var triangles = new List<int>();
        var uvs = new List<Vector2>();
        var normals = new List<Vector3>();

        var elementsCount = reader.Read<int>();
        var verticesLength = reader.Read<int>();

        for (int i = 0;i < verticesLength; i++)
        {
            vertices.Add(reader.Read<float3>());
        }

        var trianglesLength = reader.Read<int>();
        for (int i = 0; i < trianglesLength; i++)
        {
            triangles.Add(reader.Read<int>());
        }

        var uvsLength = reader.Read<int>();
        for (int i = 0; i < uvsLength; i++)
        {
            uvs.Add(reader.Read<float2>());
        }

        var normalsLength = reader.Read<int>();
        for (int i = 0; i < normalsLength; i++)
        {
            normals.Add(reader.Read<float3>());
        }

        reader.EndForEachIndex();

        Data data = new Data(vertices.ToArray(), triangles.ToArray(),uvs.ToArray(),normals.ToArray(), elementsCount);
        data.Print();

        return data;
    }

@Alex-Naronov, I suppose your problem has to do with using Vector3 in your Data struct.
I have some issues and found out about unmanaged types (didn’t heard it before) : Unmanaged types - C# reference | Microsoft Learn

Your struct should only have unmanaged types in order to make this work.

I turned Vector3 into a float array with 3 elements, for x, y and z. But it didn’t work! Arrays are not supported as well. So you will have to create independent variables…