unity runtime_error ai_generated true

ArgumentException: Tilemap position (x, y) is out of bounds. Valid range is (0, 0) to (width-1, height-1).

ID: unity/tilemap-cell-position-out-of-bounds

Also available as: JSON · Markdown · 中文
90%Fix Rate
87%Confidence
1Evidence
2023-04-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Unity 2020.3.0f1 active
Unity 2021.3.0f1 active
Unity 2022.3.0f1 active
Unity 2023.2.0f1 active

Root Cause

Attempting to set or get a tile at a position outside the Tilemap's defined bounds, often due to incorrect loop boundaries or dynamic resizing.

generic

中文

尝试在 Tilemap 定义边界之外的位置设置或获取瓦片,通常是由于循环边界错误或动态调整大小。

Official Documentation

https://docs.unity3d.com/Manual/class-Tilemap.html

Workarounds

  1. 90% success Check bounds before accessing: if (tilemap.cellBounds.Contains(new Vector3Int(x, y, 0))) { tilemap.SetTile(new Vector3Int(x, y, 0), tile); }
    Check bounds before accessing: if (tilemap.cellBounds.Contains(new Vector3Int(x, y, 0))) { tilemap.SetTile(new Vector3Int(x, y, 0), tile); }
  2. 85% success Use tilemap.HasTile(x, y) to verify existence before operations: if (tilemap.HasTile(new Vector3Int(x, y, 0))) { // proceed }
    Use tilemap.HasTile(x, y) to verify existence before operations: if (tilemap.HasTile(new Vector3Int(x, y, 0))) { // proceed }

中文步骤

  1. Check bounds before accessing: if (tilemap.cellBounds.Contains(new Vector3Int(x, y, 0))) { tilemap.SetTile(new Vector3Int(x, y, 0), tile); }
  2. Use tilemap.HasTile(x, y) to verify existence before operations: if (tilemap.HasTile(new Vector3Int(x, y, 0))) { // proceed }

Dead Ends

Common approaches that don't work:

  1. Manually resize the Tilemap in the Inspector by changing cell bounds 75% fail

    Resizing may not account for all tiles placed at edges; the error occurs at runtime due to code logic, not bounds settings.

  2. Wrap the call in a try-catch to ignore the error 90% fail

    This hides the symptom but does not prevent invalid positions, potentially corrupting tile data.