Unity 2D没有'动画'附在" xxxxxx"游戏对象

时间:2014-04-30 17:25:15

标签: exception animation unity3d

我今天开始学习2D动画,我做的是导入一个精灵,我后来切片制作爆炸帧,我创建并调整了2D动画,如果我把对象放在场景中游戏一开始就说动画循环连续不断,每个人都很开心,生活得很自由。 但是一旦我将gameobject动画设置为完成之后就不再循环,并且在完成所述游戏对象需要被销毁之后尝试对其进行编码(不需要在屏幕上显示爆炸的最终帧)我得到一个例外:“没有'动画'附加到”DefExplosion“游戏对象,但脚本正试图访问它。”

    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"); }
    }

以上脚本附加到具有动画的游戏对象上,之后需要销毁。我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我无法获取您的代码,但如果您只是在动画完成后尝试销毁对象,则可以创建一个类似

的函数
void DestroyMyGameObject(){
    Destroy(gameObject);
}

在最后一帧添加事件后的动画中,将其设置为DestroyMyGameObject() 这将在动画完成后销毁对象