Unity-Technologies/ml-agents · error · UnityAgentsException

The BufferSensor was expecting an observation of size {m_Obs

Error message

The BufferSensor was expecting an observation of size {m_ObsSize} but received {obs.Length} observations instead.

What it means

BufferSensor.AppendObservation() validates that every appended observation vector has exactly the size declared when the BufferSensor was created. A mismatched length would corrupt the fixed-size buffer tensor fed to the trainer. This is thrown immediately when a vector of the wrong length is appended.

Source

Thrown at com.unity.ml-agents/Runtime/Sensors/BufferSensor.cs:51

        /// <inheritdoc/>
        public ObservationSpec GetObservationSpec()
        {
            return m_ObservationSpec;
        }

        /// <summary>
        /// Appends an observation to the buffer. If the buffer is full (maximum number
        /// of observation is reached) the observation will be ignored. the length of
        /// the provided observation array must be equal to the observation size of
        /// the buffer sensor.
        /// </summary>
        /// <param name="obs"> The float array observation</param>
        public void AppendObservation(float[] obs)
        {
            if (obs.Length != m_ObsSize)
            {
                throw new UnityAgentsException(
                    "The BufferSensor was expecting an observation of size " +
                    $"{m_ObsSize} but received {obs.Length} observations instead."
                );
            }
            if (m_CurrentNumObservables >= m_MaxNumObs)
            {
                return;
            }
            for (int i = 0; i < obs.Length; i++)
            {
                m_ObservationBuffer[m_CurrentNumObservables * m_ObsSize + i] = obs[i];
            }
            m_CurrentNumObservables++;
        }

        /// <inheritdoc/>
        public int Write(ObservationWriter writer)
        {

View on GitHub (pinned to 3ecb446f75)

Solutions

  1. Ensure every float[] passed to AppendObservation() has exactly the same length as the buffer sensor's observation size setting
  2. Update the BufferSensorComponent's 'Observation Size' field in the inspector to match the new vector length
  3. Construct the per-entity float array with a constant/asserted length so all entities share the same feature count
  4. Add a debug assert (obs.Length == expected) in your observation-building helper to catch drift early

Example fix

// before
bufferSensorComponent.CreateBufferSensor(10, "myBuffer");
bufferSensor.AppendObservation(new float[8]); // wrong length
// after
bufferSensorComponent.CreateBufferSensor(8, "myBuffer"); // size matches
bufferSensor.AppendObservation(new float[8]);
Defensive patterns

Strategy: validation

Validate before calling

// Unity C#: before AppendObservation
if (obs.Length != expectedBufferSize)
{
    Debug.LogError($"BufferSensor expects {expectedBufferSize} floats, got {obs.Length}");
    return;
}
bufferSensor.AppendObservation(obs);

Try / catch

try
{
    bufferSensor.AppendObservation(obs);
}
catch (UnityAgentsException e) when (e.Message.Contains("BufferSensor was expecting"))
{
    Debug.LogError($"Entity observation size mismatch: {e.Message}");
}

Prevention

When it happens

Trigger: Creating BufferSensorComponent with a fixed buffer size then appending float arrays whose Length differs from that size (m_ObsSize); changing the number of features per entity (e.g. one-hot tag size or added stats) without resizing the buffer; appending observations for differently-configured entities in the same buffer.

Common situations: Adding a new field to an entity's observation vector after the buffer sensor size was set in the inspector; sharing one BufferSensorComponent across prefab variants with different entity feature counts; off-by-one when building the float array manually.

Related errors


AI-assisted analysis of Unity-Technologies/ml-agents@3ecb446f75 (2026-09-02). Data as JSON: /api/errors/eb32c004fcab8f04. Report an issue: GitHub.