How to Record Object Field References to Custom Scripts in Animation in Unity?

I have a question regarding Unity’s animation system. If I make a GameObject or Transform public in a script, I can drag a prefab asset from the project into that reference, and the animation system will record the reference during the animation recording process. However, when I make a custom script public and that prefab also has the custom script attached (or Scriptable Object), dragging the reference doesn’t allow the animation to record it.

How can I make sure that the animation system records references to my custom script in this scenario?

Unity’s documentation is a bit lacking here. If you check “Animatable properties” on the Use Animation curves manual page, you’ll only see Float, Color, Vector2, Vector3, Vector4, Quaternion and Boolean in the list of supported properties.

Animation clips obviously support some object references but Unity isn’t clear on the limitations. In the editor, you can use AnimationUtility.SetObjectReferenceCurve to try to create a curve animating any reference but it won’t work (it’ll show as missing in the Animation window and the referenced objects aren’t properly saved in the clip).

My suspicion is that Unity internally uses Class IDs to record the type of the reference. Class IDs are used for all built-in Unity classes that have a native backing type. There’s a list of Class IDs here. This would mean that it’s impossible to animate fields pointing to custom types.

One workaround is to use animation events instead. Animation events can have any Unity object reference as argument and you can add a setter method in your script to let the animation event change the property. However, this is more cumbersome to work with than normal reference curves.

Or you could establish a lookup from a float to a list of references, animate the float, and then use its value to resolve the reference in your script.