InvalidOperationException: The NativeArray has been deallocated, it is not allowed to access it

Not sure what I am doing wrong here, I am attempting to change the mesh that my MeshRenderer Component is using, it will work for a few frames then my console gets spammed with the above error.

line that is causing the error
mesh = entityManager.GetSharedComponentData(m_Data.animator*.entity);*
full script
```
*public struct Data
{
public int Length;
public ComponentDataArray animator;
[ReadOnly] public SharedComponentDataArray renderer;

}
[Inject] private Data m_Data;

protected override void OnUpdate()
{
for(int i = 0; i < m_Data.Length; i++)
{
mesh = entityManager.GetSharedComponentData(m_Data.animator[i].entity);
mesh.mesh = idleMesh[CurrentFrame];
entityManager.SetSharedComponentData(m_Data.animator[i].entity, mesh);
}
currentFrame = (currentFrame >=100) ? 0 : currentFrame = currentFrame += 1;
}*
* *here is how I am spawning the entity* *

  •    actors = new NativeArray<Entity>(100, Allocator.Persistent);
      Entity[] characters = new Entity[actors.Length];
      var entityManager = World.Active.GetOrCreateManager<EntityManager>();
      var protoType = entityManager.Instantiate(umaPrefab);
      entityManager.AddComponentData(protoType, new Position { Value = new float3(0, 0, 0) });
      entityManager.AddComponentData(protoType, new Rotation { Value = new quaternion(-90, 0, 180, 0) });
      entityManager.AddComponentData(protoType, new TransformMatrix());
      entityManager.AddComponentData(protoType, new AnimatorComponent { });
      renderer = umaPrefab.transform.Find("UMARenderer").GetComponent<SkinnedMeshRenderer>();
      entityManager.AddSharedComponentData(protoType, new MeshRenderComponent { mesh = renderer.sharedMesh, material = renderer.sharedMaterial});
    
      entityManager.Instantiate(protoType, actors);
      entityManager.DestroyEntity(protoType);
      actors.CopyTo(characters);
    
      for (int i = 0; i < characters.Length; ++i)
      {
          entityManager.SetComponentData(characters[i], new Position { Value = new float3(1 * i, 0, 1 * (i/10)) });
          entityManager.SetComponentData(characters[i], new Rotation { Value = new quaternion(0, 0, 0, 0) });
          entityManager.SetComponentData(characters[i], new AnimatorComponent { entity = characters[i] });
        
                
      }
    
    void OnDestroy()
    {
    actors.Dispose();
    }*
    ```

When you change entities via the entitiymanager directly, it invalidates the entity array, this causes the error.

Is your first script is a ComponentSystem? If yes, try to using PostUpdateCommands instead of entityManager. Also try to inject your mesh into Data if possible. GetSharedComponentData isn’t very fast.

If you use the MeshInstanceSystem.cs inside the Entity Component package as an guide they store a cached array of SharedComponents (MeshInstanceRenderer). You could do this to get the shared component then pass it into the job. I think the method is EntityManger.GetAllUniqueSharedComponentData()

This worked thank you very much for pointing me in the right direction.

I am having the same issue, but not sure how to create an entity from within MonoBehaviour within OnWillRenderObject method without an EntityManager

Same issue as Kender… Is there a way to create entities in MonoBehaviours post update?

I do not think there is a way by default, you could try adding a static method to that entities system then call it from monobehavior.

World.Active.GetOrCreateManager<EntityManager>().CreateEntity(...)

That will invalidate the entity array. He needs to create them via post update commands.

that matters only if you are inside a system.
he said MonoBehaviour, that’s outside ECS so there is nothing to invalidate.

if you are already inside a system, you just use PostUpdateCommands (or a barrier system if you are in a JobComponentSystem)

Hello, so i had a similar problem. I changed the SetSharedComponent to PostUpdateCommands, but not instead of having errors, the components don’t seem to update. I’m trying to set textures via MeshInstanceRenderer.