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
- Call MoveNext() before reading Current
- Use a foreach loop instead of manual enumerator handling
- 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
- Prefer foreach over manual enumerators
- Always call MoveNext() before reading Current
- After Reset(), MoveNext() before Current
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
- Enumerator has reached the end already.
- Index out of bounds, expected a number between 0 and {Length
- Action spaces with both continuous and discrete actions are
- Call to SendInfoToBrain when Agent hasn't been initialized.P
- Agent is already registered with a group. Unregister it firs
AI-assisted analysis of Unity-Technologies/ml-agents@3ecb446f75 (2026-09-02).
Data as JSON: /api/errors/fb782b7a4886f88e.
Report an issue: GitHub.