unity runtime_error ai_generated true

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

ArgumentOutOfRangeException: Index is out of range. Must be non-negative and less than the size of the collection. Parameter name: index in AnimationCurve.GetKey

ID: unity/argumentoutofrangeexception-animationcurve-key

其他格式: JSON · Markdown 中文 · English
88%修复率
83%置信度
1证据数
2023-07-22首次发现

版本兼容性

版本状态引入弃用备注
Unity 2022.3.12f1 active
Unity 2023.1.2f1 active
Unity 2021.3.30f1 active

根因分析

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

English

Attempting to access an AnimationCurve key at an index that does not exist, often due to modifying the curve's keys (adding/removing) without updating the index variable.

generic

官方文档

https://docs.unity3d.com/ScriptReference/AnimationCurve.GetKey.html

解决方案

  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.

无效尝试

常见但无效的做法:

  1. 70% 失败

    This hides the bug but doesn't fix the logic; the curve may still have incorrect keys.

  2. 90% 失败

    AnimationCurve may have multiple keys; using index 0 only accesses the first key, leading to wrong animation data.

  3. 95% 失败

    Wrap modes affect evaluation, not key access by index.