Add animation curve through script

Hello,

I am trying to create for a GameObject a simple animation curve in script and add it to an animation clip defined in the prefab from which the GameObject is instantiated in run-time. The following code is used:

GameObject stoneToMove = GameObject.Find("StoneBlackX6Z12");
stoneToMove.animation.GetClip("StoneMovement").ClearCurves();
Keyframe[] frames = new Keyframe[2];
frames[0] = new Keyframe { time = 1 / 60, value = stoneToMove.transform.rotation.x };
frames[1] = new Keyframe { time = 11 / 60, value = stoneToMove.transform.rotation.x - 90};
AnimationCurve animationCurveRotationX = new AnimationCurve(frames);
stoneToMove.animation.GetClip("StoneMovement").SetCurve("", typeof(Transform), "localRotation.x", animationCurveRotationX);
Debug.Log(animationCurveRotationX.keys[1].value);

Code does not generate any errors. The Debug.Log correctly prints. Clip “StoneMovement” is defined on the prefab. Nevertheless, nothing happens. If i check the Animation Viewer for my object at run-time, the curves for Rotation properties are created but only one KeyFrame at time 0 per each exists. If i create the same curve in the viewer on my prefab and only call Play in code, everything works as expected.

I had the same issue. I tried to animate the asset, not the gameobject in the scene. they both had the same name.
Is StoneBlackX6Z12 the name of the prefab in the project view?
What is the name of the prefab in the scene (hierarchy view)? Also StoneBlackX6Z12?
If so rename it in the scene to StoneBlackX6Z12_01 or something. Use this for animating.
Like GameObject.Find(“StoneBlackX6Z12_01”);

You might find this useful : Runtime Curve Editor | GUI Tools | Unity Asset Store