Unity-Technologies/ml-agents · error · UnityAgentsException

The number of training areas that you have specified exceeds

Error message

The number of training areas that you have specified exceeds the size of the grid.

What it means

TrainingAreaReplicator.AddEnvironments throws UnityAgentsException when numAreas exceeds the total grid capacity m_GridSize.x * m_GridSize.y * m_GridSize.z. The replicator lays out training area copies on a 3D grid and cannot place more areas than there are grid cells.

Source

Thrown at com.unity.ml-agents/Runtime/Areas/TrainingAreaReplicator.cs:101

            if (Academy.Instance.Communicator != null)
                numAreas = Academy.Instance.NumAreas;

            var rootNumAreas = Mathf.Pow(numAreas, 1.0f / 3.0f);
            m_GridSize.x = Mathf.CeilToInt(rootNumAreas);
            m_GridSize.y = Mathf.CeilToInt(rootNumAreas);
            var zSize = Mathf.CeilToInt((float)numAreas / (m_GridSize.x * m_GridSize.y));
            m_GridSize.z = zSize == 0 ? 1 : zSize;
        }

        /// <summary>
        /// Adds replicas of the training area to the scene.
        /// </summary>
        /// <exception cref="UnityAgentsException"></exception>
        void AddEnvironments()
        {
            if (numAreas > m_GridSize.x * m_GridSize.y * m_GridSize.z)
            {
                throw new UnityAgentsException("The number of training areas that you have specified exceeds the size of the grid.");
            }

            for (int z = 0; z < m_GridSize.z; z++)
            {
                for (int y = 0; y < m_GridSize.y; y++)
                {
                    for (int x = 0; x < m_GridSize.x; x++)
                    {
                        if (m_AreaCount == 0)
                        {
                            // Skip this first area since it already exists.
                            m_AreaCount = 1;
                        }
                        else if (m_AreaCount < numAreas)
                        {
                            m_AreaCount++;
                            var area = Instantiate(baseArea, new Vector3(x * separation, y * separation, z * separation), Quaternion.identity);
                            area.name = m_TrainingAreaName;

View on GitHub (pinned to 3ecb446f75)

Solutions

  1. Increase the Grid Size x/y/z values so their product is >= numAreas
  2. Reduce the requested number of training areas to fit within the current grid
  3. Verify the training configuration and component fields match before entering Play Mode

Example fix

// before
numAreas = 100; gridSize = (4, 4, 1); // capacity 16
// after
numAreas = 100; gridSize = (10, 10, 1); // capacity 100
Defensive patterns

Strategy: validation

Validate before calling

if (numAreas > gridSize.x * gridSize.y * gridSize.z) {
    Debug.LogError("Training areas exceed grid capacity; enlarge grid or reduce areas.");
}

Try / catch

try { replicator.enabled = true; } catch (UnityAgentsException e) { Debug.LogError(e.Message); }

Prevention

When it happens

Trigger: Setting numAreas (training areas parameter in Training Area Replicator) larger than the product of the grid size dimensions configured on the component, triggered from OnEnable.

Common situations: Increasing the training areas count in training config without enlarging the grid; misconfigured TrainingAreaReplicator component values on the training scene prefab.

Related errors


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