unity runtime_error ai_generated true

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

Also available as: JSON · Markdown · 中文
88%Fix Rate
83%Confidence
1Evidence
2023-07-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Unity 2022.3.12f1 active
Unity 2023.1.2f1 active
Unity 2021.3.30f1 active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

  1. 90% success 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.
    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. 85% success 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.
    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. 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.

Dead Ends

Common approaches that don't work:

  1. 70% fail

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

  2. 90% fail

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

  3. 95% fail

    Wrap modes affect evaluation, not key access by index.