DyamicBuffer unexpectedly empty

I am working with Unity ECS and am having trouble with DynamicBuffer. I get a reference to one using SystemAPI.GetBuffer. I can validate in the inspector that the buffer has elements, but when I get it using the query it is empty.

My hope with this post is to get some common situations in which a DynamicBuffer can be unexpectedly empty. Please post below. Hopefully the collected information will help me and potentially someone else also.

Here is a condensed snipped of my system.

foreach (var link in SystemAPI.Query<OtherEntityLink>())
{
    var linkedEntity = link.Entity;
    var buff = SystemAPI.GetBuffer<SomeBuffer>(linkedEntity);

   // buff.Length == 0
   // A foreach on the buffer has 0 iterations
}

I can of course post more code but I thought I would start with just a basic description. Here is some more context information.

  • linkedEntity is the correct entity which has the populated buffer on it. I have verified this through the inspector.
  • Initially I thought the issue might be that linkedEntity also has the Disabled component. But after testing without that component the buffer is still empty.
  • It is not a timing issue where the buffer only filled later in the frame and thus appears empty before. The buffer has elements in it for many frames before I try to query it. I can verify this through the inspector.
  • linkedEntity was originally instantiated from a prefab. It has components like EntityGuid on it.

What could be the reason for the buffer not containing any elements?

Are you sure that linkedEntity is the entity that you expected? You can verify this by taking the index of linkedEntity and use that value to search in the Entity Hierarchy window.

Another way to verify is get an instance of BufferLookup<SomeBuffer> and check if it has the buffer for linkedEntity.

Thanks for your response @davenirline. I just checked again and can confirm that linkedEntity has the correct DynamicBuffer on it.

For reasons which I truly don’t understand the buffer now suddenly has the expected elements. The only thing I fixed was a seemingly unrelated “component leak” where a component was left on an entity longer than intended and I switched around the order of a few, again seemingly unrelated, systems.

So kind of unsatisfying answer, but I guess I’ll close this for now.