# ArgumentException：Tilemap 位置 (x, y) 超出边界。有效范围为 (0, 0) 到 (width-1, height-1)。

- **ID:** `unity/tilemap-cell-position-out-of-bounds`
- **领域:** unity
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Unity 2020.3.0f1 | active | — | — |
| Unity 2021.3.0f1 | active | — | — |
| Unity 2022.3.0f1 | active | — | — |
| Unity 2023.2.0f1 | active | — | — |

## 解决方案

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 }
   ```

## 无效尝试

- **Manually resize the Tilemap in the Inspector by changing cell bounds** — Resizing may not account for all tiles placed at edges; the error occurs at runtime due to code logic, not bounds settings. (75% 失败率)
- **Wrap the call in a try-catch to ignore the error** — This hides the symptom but does not prevent invalid positions, potentially corrupting tile data. (90% 失败率)
