Unity-Technologies/UnityCsReference · error · ArgumentException

Unexpected resultBits Span length. The expected length of re

Error message

Unexpected resultBits Span length. The expected length of resultBits Span should be equal to the formula: (rendererIDs.Length + 63) / 64

What it means

Thrown by EditorCameraUtils.GetRenderersHiddenResultBits when the caller supplies a resultBits Span whose length does not match the required chunk count. The API packs one hidden/not-hidden bit per renderer into an array of 64-bit ulong chunks, so the output buffer must be sized as (rendererIDs.Length + 63) / 64 (equivalently (Length + kBitsPerChunkCount - 1) / kBitsPerChunkCount where kBitsPerChunkCount is 64). Any other length is a programming error, not a recoverable runtime state.

Source

Thrown at Editor/Mono/Camera/EditorCameraUtils.bindings.cs:50

                throw new ArgumentException("rendererIDs and results NativeArrays don't match in length.");

            GetRenderersFilteringResultsImpl(rendererIDs, results);
        }

        [System.Obsolete("GetRenderersHiddenResultBits(ReadOnlySpan<int>) is obsolete. Use GetRenderersHiddenResultBits(ReadOnlySpan<EntityId>) instead.", true)]
        public static unsafe void GetRenderersHiddenResultBits(ReadOnlySpan<int> rendererIDs, Span<ulong> resultBits)
        {
            throw new System.NotImplementedException("GetRenderersHiddenResultBits(ReadOnlySpan<int>) is obsolete. Use GetRenderersHiddenResultBits(ReadOnlySpan<EntityId>) instead.");
        }

        public static unsafe void GetRenderersHiddenResultBits(ReadOnlySpan<EntityId> rendererIDs, Span<ulong> resultBits)
        {
            const int kBitsPerChunkCount = sizeof(ulong) * 8;

            int resultBitsLengthExpected = (rendererIDs.Length + kBitsPerChunkCount - 1) / kBitsPerChunkCount;

            if (resultBitsLengthExpected != resultBits.Length)
                throw new ArgumentException("Unexpected resultBits Span length. The expected length of resultBits Span should be equal to the formula: (rendererIDs.Length + 63) / 64");

            GetRenderersHiddenResultBitsImpl(rendererIDs, resultBits);
        }

        [FreeFunction("EditorCameraUtilsScripting::RenderToCubemap")]
        static extern int RenderToCubemapImpl(Camera camera, Texture target, [DefaultValue("63")] int faceMask, StaticEditorFlags culledFlags);

        [FreeFunction("EditorCameraUtilsScripting::GetRenderersFilteringResults")]
        static extern unsafe void GetRenderersFilteringResultsImpl(ReadOnlySpan<EntityId> rendererIDs, Span<bool> results);

        [FreeFunction("EditorCameraUtilsScripting::GetRenderersHiddenResultBits")]
        static extern unsafe void GetRenderersHiddenResultBitsImpl(ReadOnlySpan<EntityId> rendererIDs, Span<ulong> resultBits);
    }
}

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Allocate resultBits as new ulong[(rendererIDs.Length + 63) / 64] right before the call, sized from the exact rendererIDs.Length you are passing.
  2. If reusing a pooled buffer, resize/rent a new one whenever rendererIDs.Length grows beyond its current chunk capacity.
  3. Wrap the size computation in a helper (e.g. int BitsToUlongChunks(int n) => (n + 63) / 64;) so the formula lives in one place and cannot drift from the call site.

Example fix

// before
var resultBits = new ulong[rendererIDs.Length];
EditorCameraUtils.GetRenderersHiddenResultBits(rendererIDs, resultBits);

// after
int chunks = (rendererIDs.Length + 63) / 64;
var resultBits = new ulong[chunks];
EditorCameraUtils.GetRenderersHiddenResultBits(rendererIDs, resultBits);
Defensive patterns

Strategy: validation

Validate before calling

static int UlongChunksForRenderers(int rendererCount) => (rendererCount + 63) / 64;

// before the call:
int expected = UlongChunksForRenderers(rendererIDs.Length);
if (resultBits.Length != expected)
    resultBits = new ulong[expected];

Type guard

static bool IsValidResultBits(ReadOnlySpan<EntityId> rendererIDs, Span<ulong> resultBits)
    => resultBits.Length == (rendererIDs.Length + 63) / 64;

Prevention

When it happens

Trigger: Calling GetRenderersHiddenResultBits(ReadOnlySpan<EntityId> rendererIDs, Span<ulong> resultBits) with a resultBits buffer whose Length differs from (rendererIDs.Length + 63) / 64. Common off-by cases: passing a buffer of the same Length as rendererIDs (treating bytes as bits), passing a single ulong for any number of renderers, or reusing a buffer sized for a previous rendererIDs array whose Length changed.

Common situations: Editor tooling that batches visibility queries (e.g. occlusion/LOD inspection scripts) where the renderer list size is dynamic but the output buffer is allocated once. Migrating from the obsolete ReadOnlySpan<int> overload to the EntityId overload and forgetting to recompute the buffer size. Copy-paste that hardcodes the chunk count as the renderer count.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/bebe9cb3ce376015. Report an issue: GitHub.