Unity-Technologies/ml-agents · error · InvalidOperationException

Enumerator not started.

Error message

Enumerator not started.

What it means

The ActionSegment<T>.Enumerator's Current getter throws InvalidOperationException when the enumerator has not been started, i.e. Current is accessed before the first MoveNext() call (m_Current < m_Start). This mirrors standard .NET enumerator semantics and indicates misuse of the enumeration protocol.

Source

Thrown at com.unity.ml-agents/Runtime/Actuators/ActionSegment.cs:215

                m_Current = arraySegment.Offset - 1;
            }

            public bool MoveNext()
            {
                if (m_Current < m_End)
                {
                    m_Current++;
                    return m_Current < m_End;
                }
                return false;
            }

            public T Current
            {
                get
                {
                    if (m_Current < m_Start)
                        throw new InvalidOperationException("Enumerator not started.");
                    if (m_Current >= m_End)
                        throw new InvalidOperationException("Enumerator has reached the end already.");
                    return m_Array[m_Current];
                }
            }

            object IEnumerator.Current => Current;

            void IEnumerator.Reset()
            {
                m_Current = m_Start - 1;
            }

            public void Dispose()
            {
            }
        }
    }

View on GitHub (pinned to 3ecb446f75)

Solutions

  1. Call MoveNext() before reading Current
  2. Use a foreach loop instead of manual enumerator handling
  3. If using Reset(), always MoveNext() again before reading Current

Example fix

// before
var e = actions.GetEnumerator();
var first = e.Current;
// after
var e = actions.GetEnumerator();
if (e.MoveNext()) { var first = e.Current; }
Defensive patterns

Strategy: try-catch

Validate before calling

var e = actions.GetEnumerator();
if (e.MoveNext()) { var v = e.Current; }

Try / catch

try { var v = e.Current; } catch (InvalidOperationException) { /* enumerator not started; call MoveNext */ }

Prevention

When it happens

Trigger: Accessing enumerator.Current (or foreach underlying misuse) without calling MoveNext() first, or manually resetting with Reset() then reading Current before MoveNext().

Common situations: Custom iteration code that grabs Current immediately after GetEnumerator(); implementing manual enumerator loops instead of using foreach.

Related errors


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