Search Unity

How to play an animation from within a script

Discussion in 'Scripting' started by Aurecon_Unity, Jul 26, 2011.

  1. Aurecon_Unity

    Aurecon_Unity

    Joined:
    Jul 6, 2011
    Posts:
    241
    Hi, this may seem like an easy thing to do, but I basically want to toggle an animation speed through a script attached to an empty gameobject.

    So, here is what I have:

    1 Object ("Camera_Animation")
    1 Animation Clip ("Take 001")

    I can script it fine if I attach the script to the object and use animation["Take 001"].speed = 0;

    My problem is when I want to attach the script to another gameobject. I'm not sure of the syntax needed to store "Take 001" in a variable to be called up on its own.

    This is what I have, but it doesn't work

    Code (csharp):
    1. var AnimObject : GameObject;
    2.  
    3. function Update () {
    4.  
    5. if (Input.GetKeyDown ("m")) {
    6.     AnimObject.animation["Take 001"].speed = 0;
    7.     }
    8.  
    9. }
    Thanks
     
    Last edited: Jul 26, 2011
  2. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347