Search Unity

Weird bug with getting dynamic buffers

Discussion in 'Entity Component System' started by 8bitgoose, Oct 3, 2019.

  1. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    448
    Hello all,

    This returns an error

    Code (CSharp):
    1.  
    2. DynamicBuffer<SailDisplayPoint> sailPointsBuffer = EntityManager.AddBuffer<SailDisplayPoint>(headEntity);
    3. DynamicBuffer<SailDisplayTri> sailTrisBuffer = EntityManager.AddBuffer<SailDisplayTri>(headEntity);
    4.  
    5. for(int i = 0, l = entities.Length; i < l; i++)
    6.    sailPointsBuffer.Add(new SailDisplayPoint() { Entity = entities[i] });
    7.  
    "InvalidOperationException: The NativeArray has been deallocated, it is not allowed to access it" on the last line BUT

    Code (CSharp):
    1.  
    2. DynamicBuffer<SailDisplayPoint> sailPointsBuffer = EntityManager.AddBuffer<SailDisplayPoint>(headEntity);
    3. //DynamicBuffer<SailDisplayTri> sailTrisBuffer = EntityManager.AddBuffer<SailDisplayTri>(headEntity);
    4.  
    5. for(int i = 0, l = entities.Length; i < l; i++)
    6.    sailPointsBuffer.Add(new SailDisplayPoint() { Entity = entities[i] });
    7.  
    I have not referenced sailTrisBuffer at all and can't see what I am doing wrong. Can there only be one Dynamic Buffer per entity?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Any structural change call to EntityManager invalidates arrays.

    Simply need to call getbuffer after you add the second buffer instead.
     
  3. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    448
    Ah makes sense! Thanks as always for the help.