# 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`
- **Domain:** unity
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 88%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Unity 2022.3.12f1 | active | — | — |
| Unity 2023.1.2f1 | active | — | — |
| Unity 2021.3.30f1 | active | — | — |

## Workarounds

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.** (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.
   ```
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.** (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.
   ```

## Dead Ends

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