Using an animation clip to drive a variable on a component attached at runtime?

At runtime a ‘player’ object gets created dynamically, and during the game, in some cases a certain component (a ‘powerup’ script) gets attached that defines a ‘power’ variable, and then uses this variable to influence the player object it’s attached to. From inside the powerup script I’d like to be able to trigger the ‘animation’ of this variable so that its value follows a predefined animation curve.

I’m having a hard time figuring out if this is possible, and if so, how to go about arranging it all. Any pointers would be greatly appreciated.

  1. I can create an animation clip if I temporarily attach the powerup script to an empty game object and use the animation window to draw a curve that changes the power variable. I then delete the empty game object, leaving the animation in my project window (‘gustCurve’). Is this a sensible way to begin?

  2. Is there a way, from the code inside the powerup script, to then attach gustCurve to the player object, so that when it plays, it will modify ‘power’ on the powerup script?

Thanks in advance for any pointers you can give!

A workaround is to instantiate a prefab that deals with creating the effect of the power up when used.

The prefab object has a script pre-attached, and an animation that interacts with variables in that script (‘eg power’).

If necessary, a script attached to the player can store a reference to this new object (or vice versa) if further interaction between the player and object are necessary.