【发布时间】:2013-12-02 16:54:42
【问题描述】:
我正在为 2d 游戏尝试新的 unity 3d 选项。我正在尝试创建根据我所做的操作而变化的背景。因此,如果我按下按钮一,我将精灵一作为背景,如果二,我将精灵二。由于我有 32 个选项,我发现最好的方法是拥有一个 Animator,它会根据按钮单击来更改帧。所以我创建了动画师和动画。但问题是我无法设置时间来停止动画以显示所选帧。
我正在尝试这样:
Animator ani=background.GetComponent<Animator>();
ani.animation["field_back_anim"].time=0.5f;
ani.speed=0;
但它在第二行失败并出现该错误:
MissingComponentException: There is no 'Animation' attached to the "background" game object, but a script is trying to access it.
You probably need to add a Animation to the game object "background". Or your script needs to check if the component is attached before using it.
但是,如果我不编写代码,动画只会播放所有 16 帧。所以我想毕竟有动画。
在背景游戏对象上,我有 2 个组件,第一个是精灵渲染器,第二个是动画师。当我在 Animator 视图中打开 animator 时,我看到绿色矩形显示任何状态,黄色矩形显示“field_back_anim”。我不明白我做错了什么。
我还将排除执行以下操作的任何其他解决方案。
谢谢!
【问题讨论】:
-
如何获取代码中的背景对象?
-
我将它作为公共游戏对象添加到检查器中。