Can't change keyframe values in script

Hi everyone !
I’m facing a strange and annoying issue while working with keyframe. It’s pretty simple : I just CANT change any keyframe (value / time), even if the AnimationCurve is created by code or in the inspector.

using UnityEngine;
using System.Collections;

public class KeyframeTest : MonoBehaviour {

    public AnimationCurve    testCurve ;

    // Use this for initialization
    void Start ()
    {
        testCurve = new AnimationCurve() ;
        testCurve.AddKey(0f,1f) ;
        testCurve.AddKey(0.5f,0f) ;
        testCurve.AddKey(1f,1f) ;

        Debug.Log(testCurve.keys[1].value+"   "+testCurve.keys[1].time) ;
        testCurve.keys[1].value = 10f ;
        testCurve.keys[1].time = 0.7f ;
        Debug.Log(testCurve.keys[1].value+"   "+testCurve.keys[1].time) ;
    }

}

I’m using Unity 4.3.3f1, if someone can check this with a different version it will be awesome :wink: !

According to the documentation:

I could be wrong, but I think that means that when you do this:

        testCurve.keys[1].value = 10f ;

The AnimationCurve makes a copy of its keys, sends it to you; you then modify one key, but you’ve only modified it in the copy (which is immediately discarded from memory, since nothing got assigned to it). Try doing this:

Keyframe[] tempKeys = testCurve.keys;
tempKeys[1].value = 10f;
tempKeys[1].time = 0.7f;
testCurve.keys = tempKeys;
1 Like

Shame on me, I haven’t read this part of the documentation and I didn’t thought about the fact that the array will be do a copy by value.

Thanks a lot !