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 :
public class ExplosionEnd : MonoBehaviour {
Animation Anim;
// Use this for initialization
void Start ()
{
Anim = GetComponent<Animation>();
gameObject.GetComponent<Animator>();
gameObject.GetComponent<Animation>();
}
// Update is called once per frame
void Update ()
{
if (!gameObject.animation.isPlaying)
{ Destroy(gameObject); Debug.Log("Destroyed"); }
}
the above script is attached to the gameobject that has the animation and need to be destroyed afterwards… what am I missing here ?
PS: don’t answer to say “Have you just tried to get rid of everything and use Anim = GetComponent(); ?” because that’s the first thing I actually tried to do