Unity-Technologies/ml-agents · error · IndexOutOfRangeException
width value must be in range [0, ]
Error message
width value {w} must be in range [0, {m_TensorShape.Width() - 1}] What it means
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.
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)
Example fix
// before for (int w = 0; w <= tex.width; w++) writer[0, w, 0] = pixel; // after for (int w = 0; w < obsSpec.Width(); w++) writer[0, w, 0] = pixel;
Defensive patterns
Strategy: validation
Validate before calling
// Unity C#: in custom sensor Write
if (w < 0 || w >= obsSpec.Width())
{
Debug.LogError($"w={w} outside [0,{obsSpec.Width() - 1}]");
return;
}
writer[h, w, ch] = value; Try / catch
try
{
writer[h, w, ch] = value;
}
catch (IndexOutOfRangeException e) when (e.Message.Contains("width value"))
{
Debug.LogError("Width out of bounds writing observation: " + e.Message);
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- channel value must be in range [0, ]
- height value must be in range [0, ]
- Cannot move cell at row=
- col was , but must be between 0 and .
- row was , but must be between 0 and .
AI-assisted analysis of Unity-Technologies/ml-agents@3ecb446f75 (2026-09-02).
Data as JSON: /api/errors/f60bd2b2732f892d.
Report an issue: GitHub.
Appendix: source
Thrown at com.unity.ml-agents/Runtime/Sensors/ObservationWriter.cs:147
/// 3D write access at the specified height, width, and channel.
/// </summary>
/// <param name="h">Height</param>
/// <param name="w">Width</param>
/// <param name="ch">Channels</param>
public float this[int ch, int h, int w]
{
set
{
if (m_Data != null)
{
if (h < 0 || h >= m_TensorShape.Height())
{
throw new IndexOutOfRangeException($"height value {h} must be in range [0, {m_TensorShape.Height() - 1}]");
}
if (w < 0 || w >= m_TensorShape.Width())
{
throw new IndexOutOfRangeException($"width value {w} must be in range [0, {m_TensorShape.Width() - 1}]");
}
if (ch < 0 || ch >= m_TensorShape.Channels())
{
throw new IndexOutOfRangeException($"channel value {ch} must be in range [0, {m_TensorShape.Channels() - 1}]");
}
var index = m_TensorShape.Index(m_Batch, ch + m_Offset, h, w);
m_Data[index] = value;
}
else
{
if (ch + m_Offset < 0 || ch + m_Offset >= m_TensorShape.Channels() ||
h < 0 || h >= m_TensorShape.Height() ||
w < 0 || w >= m_TensorShape.Width())
return;
m_Proxy.data.CompleteAllPendingOperations();View on GitHub (pinned to 3ecb446f75)