Unity-Technologies/ml-agents · error · IndexOutOfRangeException
height value {h} must be in range [0, {m_TensorShape.Height(
Error message
height value {h} must be in range [0, {m_TensorShape.Height() - 1}] What it means
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.
Source
Thrown at com.unity.ml-agents/Runtime/Sensors/ObservationWriter.cs:142
}
}
}
/// <summary>
/// 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() ||View on GitHub (pinned to 3ecb446f75)
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)
Example fix
// before for (int h = 0; h <= tex.height; h++) writer[h, 0, 0] = pixel; // after for (int h = 0; h < obsSpec.Height(); h++) writer[h, 0, 0] = pixel;
Defensive patterns
Strategy: validation
Validate before calling
// Unity C#: in custom sensor Write
if (h < 0 || h >= obsSpec.Height())
{
Debug.LogError($"h={h} outside [0,{obsSpec.Height() - 1}]");
return;
}
writer[h, w, ch] = value; Try / catch
try
{
writer[h, w, ch] = value;
}
catch (IndexOutOfRangeException e) when (e.Message.Contains("height value"))
{
Debug.LogError("Height out of bounds writing observation: " + e.Message);
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- width value {w} must be in range [0, {m_TensorShape.Width()
- channel value {ch} must be in range [0, {m_TensorShape.Chann
- row was {row}, but must be between 0 and {maxBoardSize.Rows
- col was {col}, but must be between 0 and {maxBoardSize.Colum
- Cannot move cell at row={row} col={col} in Direction={dir}
AI-assisted analysis of Unity-Technologies/ml-agents@3ecb446f75 (2026-09-02).
Data as JSON: /api/errors/b669dffe3b3732ba.
Report an issue: GitHub.