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

2D Animation Help, noob question

Discussion in 'Scripting' started by Rphysx, Apr 30, 2014.

  1. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    Ok I Started today to learn 2D animations and what I did was importing a sprite that I later sliced to make the frames of an explosion, I created and adjusted a 2D animation out of it and if I put the object in the scene as soon as the game start said animation loops continuosly and everybody is happy and lives free.
    But as soon as I set the gameobject animation not to loop after it has finished, and try to code that after it finishes said gameobject needs to be destroyed (no need to have the final frame of an explosion on the screen) I get an exception : "There is no 'Animation' attached to the "DefExplosion" game object, but a script is trying to access it."

    Here's what I've tried so far :
    Code (csharp):
    1.  
    2.  
    3. public class ExplosionEnd : MonoBehaviour {
    4.  
    5.     Animation Anim;
    6.  
    7.     // Use this for initialization
    8.     void Start ()
    9.     {
    10.         Anim = GetComponent<Animation>();
    11.         gameObject.GetComponent<Animator>();
    12.         gameObject.GetComponent<Animation>();
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.         if (!gameObject.animation.isPlaying)
    19.         { Destroy(gameObject); Debug.Log("Destroyed"); }
    20.     }
    21.  
    22.  
    the above script is attached to the gameobject that has the animation and need to be destroyed afterwards.. what am I missing here ?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Code (csharp):
    1.  
    2.         Anim = GetComponent<Animation>();
    3.  
    4.         gameObject.GetComponent<Animator>();
    5.  
    6.         gameObject.GetComponent<Animation>();
    7.  
    You're mixing up the legacy animation system with the current one, probably. ("Animation" is a legacy component; "Animator" is the modern one, and the one that is created by default anytime you create an animation.) It looks like a couple of aborted attempts to fix it were made, although the second and third GetComponent calls are just sort of floating in the wind, and getting no results. (The faucet is on, but you don't have a cup underneath it to catch the water)

    Try Anim = GetComponent<Animator>(); in place of all of those.
     
  3. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    That was the first thing I did, with no results and same error showing
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Could you upload a screenshot of the Inspector view of this object? It'd help to see how this is set up to figure out the problem.
     
  5. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54

    Attached Files:

    • $dfd.png
      $dfd.png
      File size:
      39.9 KB
      Views:
      775
    Last edited: Apr 30, 2014
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Are you sure you got the same error when you use GetComponent<Animator>()?
     
  7. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    Definitely. IsPlaying function won't return anything a long as it keeps missing the animation reference. If you need more screenshots just tell me
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Oh, I figured it out. Even when you change your "animation" variable to be an Animator instead, you're still using:

    Code (csharp):
    1.  
    2.         if (!gameObject.animation.isPlaying)
    3.  
    which is the gameObject's animation, which is still an Animation and not an Animator.

    (Which is to say: if you fix that, you'll have new and exciting errors! But probably more helpful, fixable ones.)
     
  9. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    So I should call another function instead of that one ? or pheraps casting it to Anim ? I don't even know if mechanim has a IsPlaying boolean value... I find this new system not only confusing but with half of the supposed features in it