{"record":{"id":"bebe9cb3ce376015","repo":"Unity-Technologies/UnityCsReference","slug":"unexpected-resultbits-span-length-the-expected-le","errorCode":null,"errorMessage":"Unexpected resultBits Span length. The expected length of resultBits Span should be equal to the formula: (rendererIDs.Length + 63) / 64","messagePattern":"Unexpected resultBits Span length\\. The expected length of resultBits Span should be equal to the formula: \\(rendererIDs\\.Length \\+ 63\\) / 64","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Camera/EditorCameraUtils.bindings.cs","lineNumber":50,"sourceCode":"                throw new ArgumentException(\"rendererIDs and results NativeArrays don't match in length.\");\n\n            GetRenderersFilteringResultsImpl(rendererIDs, results);\n        }\n\n        [System.Obsolete(\"GetRenderersHiddenResultBits(ReadOnlySpan<int>) is obsolete. Use GetRenderersHiddenResultBits(ReadOnlySpan<EntityId>) instead.\", true)]\n        public static unsafe void GetRenderersHiddenResultBits(ReadOnlySpan<int> rendererIDs, Span<ulong> resultBits)\n        {\n            throw new System.NotImplementedException(\"GetRenderersHiddenResultBits(ReadOnlySpan<int>) is obsolete. Use GetRenderersHiddenResultBits(ReadOnlySpan<EntityId>) instead.\");\n        }\n\n        public static unsafe void GetRenderersHiddenResultBits(ReadOnlySpan<EntityId> rendererIDs, Span<ulong> resultBits)\n        {\n            const int kBitsPerChunkCount = sizeof(ulong) * 8;\n\n            int resultBitsLengthExpected = (rendererIDs.Length + kBitsPerChunkCount - 1) / kBitsPerChunkCount;\n\n            if (resultBitsLengthExpected != resultBits.Length)\n                throw new ArgumentException(\"Unexpected resultBits Span length. The expected length of resultBits Span should be equal to the formula: (rendererIDs.Length + 63) / 64\");\n\n            GetRenderersHiddenResultBitsImpl(rendererIDs, resultBits);\n        }\n\n        [FreeFunction(\"EditorCameraUtilsScripting::RenderToCubemap\")]\n        static extern int RenderToCubemapImpl(Camera camera, Texture target, [DefaultValue(\"63\")] int faceMask, StaticEditorFlags culledFlags);\n\n        [FreeFunction(\"EditorCameraUtilsScripting::GetRenderersFilteringResults\")]\n        static extern unsafe void GetRenderersFilteringResultsImpl(ReadOnlySpan<EntityId> rendererIDs, Span<bool> results);\n\n        [FreeFunction(\"EditorCameraUtilsScripting::GetRenderersHiddenResultBits\")]\n        static extern unsafe void GetRenderersHiddenResultBitsImpl(ReadOnlySpan<EntityId> rendererIDs, Span<ulong> resultBits);\n    }\n}\n","sourceCodeStart":32,"sourceCodeEnd":65,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Camera/EditorCameraUtils.bindings.cs#L32-L65","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Allocate resultBits as new ulong[(rendererIDs.Length + 63) / 64] right before the call, sized from the exact rendererIDs.Length you are passing.","If reusing a pooled buffer, resize/rent a new one whenever rendererIDs.Length grows beyond its current chunk capacity.","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."],"exampleFix":"// before\nvar resultBits = new ulong[rendererIDs.Length];\nEditorCameraUtils.GetRenderersHiddenResultBits(rendererIDs, resultBits);\n\n// after\nint chunks = (rendererIDs.Length + 63) / 64;\nvar resultBits = new ulong[chunks];\nEditorCameraUtils.GetRenderersHiddenResultBits(rendererIDs, resultBits);","handlingStrategy":"validation","validationCode":"static int UlongChunksForRenderers(int rendererCount) => (rendererCount + 63) / 64;\n\n// before the call:\nint expected = UlongChunksForRenderers(rendererIDs.Length);\nif (resultBits.Length != expected)\n    resultBits = new ulong[expected];","typeGuard":"static bool IsValidResultBits(ReadOnlySpan<EntityId> rendererIDs, Span<ulong> resultBits)\n    => resultBits.Length == (rendererIDs.Length + 63) / 64;","tryCatchPattern":null,"preventionTips":["Always allocate resultBits from the same rendererIDs.Length you pass to the call.","Centralize the chunk formula in one helper to avoid drift.","Resize pooled buffers when the renderer list grows."],"tags":["unity-editor","bindings","span","argument-validation","rendering"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}