【问题标题】:Unity2D: MissingComponentException: There is no 'Animation' attached to the "Arm" game objectUnity 2D:缺少组件异常:“手臂”游戏对象没有附加“动画”
【发布时间】:2014-12-05 16:59:21
【问题描述】:

我正在尝试使用此代码在每次按下空格键时出现动画:

using UnityEngine;
using System.Collections;

public class ArmSwipe : MonoBehaviour {

    Animation me;

    void Start (){
        me=GetComponent<Animation>();
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(KeyCode.Space)){
            me.Play();
        }
    }
}

每次运行都会出现这个错误

MissingComponentException:“Arm”游戏对象没有附加“动画”,但脚本正在尝试访问它。 您可能需要向游戏对象“Arm”添加一个动画。或者您的脚本需要在使用之前检查组件是否已附加。

UnityEngine.Animation.Play (PlayMode mode)
UnityEngine.Animation.Play ()
ArmSwipe.Update () (at Assets/Scripts/ArmSwipe.cs:18)

编辑:

【问题讨论】:

    标签: animation unity3d


    【解决方案1】:

    您的 GameObject Arm 需要附加一个动画。您似乎没有将此类动画附加到 Arm。您能否附上您的场景图和 Arm 对象的检查器属性的屏幕截图?

    此外,您可能应该使用Input.GetKeyUp 而不是像您目前使用的 KeyDown。 KeyDown 只会导致动画在每次更新时重新播放,并且看起来像是冻结或卡顿。

    【讨论】:

    • 我添加了我认为会有帮助的图片
    • 看起来您在 mecanim 动画方法(动画控制器,状态机以根据脚本中设置的触发器/值处理动画)和传统动画方法(脚本直接调用动画)之间混淆了。您可能需要重新访问 Unity 网站上的学习部分并查看 mecanim 教程。
    猜你喜欢
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    相关资源
    最近更新 更多