{"record":{"id":"f60bd2b2732f892d","repo":"Unity-Technologies/ml-agents","slug":"width-value-w-must-be-in-range-0-m-tensorshap","errorCode":null,"errorMessage":"width value {w} must be in range [0, {m_TensorShape.Width() - 1}]","messagePattern":"width value (.+?) must be in range \\[0, (.+?)\\]","errorType":"validation","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"com.unity.ml-agents/Runtime/Sensors/ObservationWriter.cs","lineNumber":147,"sourceCode":"        /// 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() ||\n                        h < 0 || h >= m_TensorShape.Height() ||\n                        w < 0 || w >= m_TensorShape.Width())\n                        return;\n\n                    m_Proxy.data.CompleteAllPendingOperations();","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/com.unity.ml-agents/Runtime/Sensors/ObservationWriter.cs#L129-L165","documentation":"ObservationWriter's indexed setter validates that the width coordinate falls within the observation tensor's width before writing. Writing outside that range would corrupt adjacent data in the flat backing array, so an IndexOutOfRangeException is thrown.","triggerScenarios":"A custom sensor's Write() implementation calling Write(h, w, ch, value) with w < 0 or w >= observation width; iterating source texture columns wider than the declared observation width; off-by-one loop bounds.","commonSituations":"Custom visual sensors whose render texture is wider than the observation spec width; writing in column-major order with wrong limits; resizing an observation without updating the writing loops.","solutions":["Clamp loop bounds to the observation spec's width: for (int w = 0; w < spec.Width(); w++)","Use ObservationWriter helper APIs (WriteTex2D, Add) that stay within the tensor bounds","Ensure ObservationSpec width matches the actual data layout being written","Check for off-by-one loops (w <= width instead of w < width)"],"exampleFix":"// before\nfor (int w = 0; w <= tex.width; w++) writer[0, w, 0] = pixel;\n// after\nfor (int w = 0; w < obsSpec.Width(); w++) writer[0, w, 0] = pixel;","handlingStrategy":"validation","validationCode":"// Unity C#: in custom sensor Write\nif (w < 0 || w >= obsSpec.Width())\n{\n    Debug.LogError($\"w={w} outside [0,{obsSpec.Width() - 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(\"width value\"))\n{\n    Debug.LogError(\"Width out of bounds writing observation: \" + e.Message);\n}","preventionTips":["Derive write-loop bounds from spec.Width(), not source texture dimensions","Watch for off-by-one (<=) loop conditions","Use ObservationWriter helper methods that respect tensor 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"}