# ArgumentOutOfRangeException：索引超出范围。必须为非负数且小于集合大小。参数名：index 在 AnimationCurve.GetKey 中

- **ID:** `unity/argumentoutofrangeexception-animationcurve-key`
- **领域:** unity
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

尝试访问 AnimationCurve 中不存在的索引处的键，通常是由于修改了曲线的键（添加/删除）而没有更新索引变量。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Unity 2022.3.12f1 | active | — | — |
| Unity 2023.1.2f1 | active | — | — |
| Unity 2021.3.30f1 | active | — | — |

## 解决方案

1. ```
   Before accessing a key, check the curve's key count and ensure the index is valid. Use curve.keys.Length to get the count and clamp the index.
   ```
2. ```
   If dynamically adding keys, use curve.AddKey() which returns the index of the new key; store that index for later access instead of assuming a fixed index.
   ```

## 无效尝试

- **** — This hides the bug but doesn't fix the logic; the curve may still have incorrect keys. (70% 失败率)
- **** — AnimationCurve may have multiple keys; using index 0 only accesses the first key, leading to wrong animation data. (90% 失败率)
- **** — Wrap modes affect evaluation, not key access by index. (95% 失败率)
