Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[Help Please] Death Animation

Discussion in 'Scripting' started by RandomActStudios, Aug 14, 2014.

  1. RandomActStudios

    RandomActStudios

    Joined:
    Jul 21, 2014
    Posts:
    15
    I have made it so that when I click it sends a raycast down and deals damage to the object and destroys it when it runs out of health. But then I tried making it so that it plays an animation when it is destroyed. That is where things started to go wrong, when I did this in my coding it made it so that now it no longer destroys the object and hence doesn't transition to the other animation. Does anyone know what I am doing wrong?
    Here is the error I am getting...
    ComponentException: There is no 'Animation' attached to the "MenuSheep" game object, but a script is trying to access it.
    You probably need to add a Animation to the game object "MenuSheep". Or your script needs to check if the component is attached before using it

    Here is my coding...
    Code (JavaScript):
    1. #pragma strict
    2. var ourObject : GameObject;
    3. var currentHealth : float = 0.0f;
    4. var maximumHealth : float = 15.0f;
    5.  
    6.  
    7. function Start () {
    8.     currentHealth = maximumHealth;
    9.     }
    10.  
    11. function ApplyDamage (bulletDamage : float) {
    12.     currentHealth -= bulletDamage;
    13.     if(currentHealth <= 0) {
    14.             GameObject.Find("MenuSheep").animation.Play("DeathAnimation");
    15.             Destroytimer ();
    16.             }
    17.     }
    18. function Destroytimer (){
    19.     yield WaitForSeconds (animation["DeathAnimation"].length);
    20.     Destroy(ourObject);
    21.     }
    Not sure what I am doing wrong :(
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    You need to add an Animation component to your "MenuSheep" gameObject.

    Select it and go to AddComponent > Miscellaneous > Animation.
     
  3. RandomActStudios

    RandomActStudios

    Joined:
    Jul 21, 2014
    Posts:
    15
    I have done so but when I do it automatically plays the animation I attach to it instead of waiting for it to be destroyed
     
  4. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Try turning off "Play Automatically" on your Animation component.
     
  5. RandomActStudios

    RandomActStudios

    Joined:
    Jul 21, 2014
    Posts:
    15
    I am now getting the error...
    The animation state DeathAnimation could not be played because it couldn't be found!
    Please attach an animation clip with the name 'DeathAnimation' or call this function only for existing animations.
    ... when I try to click on the object I am destroying
     
  6. RandomActStudios

    RandomActStudios

    Joined:
    Jul 21, 2014
    Posts:
    15
    Alright I figured out that part it was saying I had to change the Animation to legacy, now it is back to destroying the object but is still not creating the animation after the object is destroyed