{"record":{"id":"b669dffe3b3732ba","repo":"Unity-Technologies/ml-agents","slug":"height-value-h-must-be-in-range-0-m-tensorsha","errorCode":null,"errorMessage":"height value {h} must be in range [0, {m_TensorShape.Height() - 1}]","messagePattern":"height value (.+?) must be in range \\[0, (.+?)\\]","errorType":"validation","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"com.unity.ml-agents/Runtime/Sensors/ObservationWriter.cs","lineNumber":142,"sourceCode":"                }\n            }\n        }\n\n        /// <summary>\n        /// 3D write access at the specified height, width, and channel.\n        /// </summary>\n        /// <param name=\"h\">Height</param>\n        /// <param name=\"w\">Width</param>\n        /// <param name=\"ch\">Channels</param>\n        public float this[int ch, int h, int w]\n        {\n            set\n            {\n                if (m_Data != null)\n                {\n                    if (h < 0 || h >= m_TensorShape.Height())\n                    {\n                        throw new IndexOutOfRangeException($\"height value {h} must be in range [0, {m_TensorShape.Height() - 1}]\");\n                    }\n\n                    if (w < 0 || w >= m_TensorShape.Width())\n                    {\n                        throw new IndexOutOfRangeException($\"width value {w} must be in range [0, {m_TensorShape.Width() - 1}]\");\n                    }\n\n                    if (ch < 0 || ch >= m_TensorShape.Channels())\n                    {\n                        throw new IndexOutOfRangeException($\"channel value {ch} must be in range [0, {m_TensorShape.Channels() - 1}]\");\n                    }\n\n                    var index = m_TensorShape.Index(m_Batch, ch + m_Offset, h, w);\n                    m_Data[index] = value;\n                }\n                else\n                {\n                    if (ch + m_Offset < 0 || ch + m_Offset >= m_TensorShape.Channels() ||","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/com.unity.ml-agents/Runtime/Sensors/ObservationWriter.cs#L124-L160","documentation":"ObservationWriter's indexed setter validates that the height coordinate falls within the observation tensor's height before writing. Writing outside that range would corrupt adjacent observations in the underlying flat data array, so an IndexOutOfRangeException is thrown.","triggerScenarios":"A custom sensor's Write() implementation calling Write(h, w, ch, value) with h < 0 or h >= observation height (e.g. looping over a texture's full height while the writer's tensor has smaller height); confusing raw texture coordinates with observation tensor coordinates.","commonSituations":"Custom visual sensors where the render texture is larger than the declared observation height; iterating rows of a source texture instead of the observation tensor; off-by-one using <= in loop bounds.","solutions":["Clamp loop bounds to the observation spec's height: for (int h = 0; h < spec.Height(); h++)","Use foreach (var h in ...) style helpers like ObservationWriter.WriteTex2D or AddList/Add that handle bounds internally","Verify the sensor's ObservationSpec height matches the data actually being written (e.g. after resizing render textures)","Check for off-by-one loops (h <= height instead of h < height)"],"exampleFix":"// before\nfor (int h = 0; h <= tex.height; h++) writer[h, 0, 0] = pixel;\n// after\nfor (int h = 0; h < obsSpec.Height(); h++) writer[h, 0, 0] = pixel;","handlingStrategy":"validation","validationCode":"// Unity C#: in custom sensor Write\nif (h < 0 || h >= obsSpec.Height())\n{\n    Debug.LogError($\"h={h} outside [0,{obsSpec.Height() - 1}]\");\n    return;\n}\nwriter[h, w, ch] = value;","typeGuard":null,"tryCatchPattern":"try\n{\n    writer[h, w, ch] = value;\n}\ncatch (IndexOutOfRangeException e) when (e.Message.Contains(\"height value\"))\n{\n    Debug.LogError(\"Height out of bounds writing observation: \" + e.Message);\n}","preventionTips":["Derive write-loop bounds from spec.Height(), not from the source texture size","Watch for off-by-one (<=) loop conditions","Use ObservationWriter helper methods (WriteTex2D/Add) that handle bounds"],"tags":["unity","ml-agents","observation-writer","bounds","index-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}