我很确定
animation.Play("DoorOpen");
会播放动画“DoorOpen”,但是当我试图把它放在我的代码中时,它只是给我一个错误信息:
附加到此GameObject的动画(如果没有附加,则为null)。
using UnityEngine;
using System.Collections;
public class DoorPhysics : MonoBehaviour {
int Open = 0;
// Update is called once per frame
void Update() {
if (Open == 0) {
if (Input.GetKeyDown("e")) {
animation.Play("DoorOpen");
}
}
}
}
答案 0 :(得分:0)
你需要统一显示游戏对象的位置,他们不知道彼此,你必须经常使用:
GameObject.GetComponent<T>()
GetComponentInParent<T>()
GetComponentInChildren<T>()
最佳做法是在Start()
获取对象引用
你还应该将重要!!! 动画组件附加到它附加到此脚本的对象
public class DoorPhysics : MonoBehaviour {
public Animation animation;
int Open = 0;
void Start()
{
animation=GameObject.GetComponent<Animation>(); //if your have derived type change Animation to good class DoorAnimation for example
}
void Update()
{
if (Open == 0) {
if (Input.GetKeyDown("e")) {
this.animation.Play("DoorOpen");
}
}
}
}
如果该代码不起作用,您需要向我展示您的GameObject层次结构
如果您刚开始旅行,请了解MonoBehaviour call order 和life cycles of events