gpt4 book ai didi

c# - animation.Play() 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:31:42 24 4
gpt4 key购买 nike

我很确定

animation.Play("DoorOpen");

会播放动画“DoorOpen”,但是当我试图将它放入我的代码中时,它只是给我一条错误消息:

The Animation attached to this GameObject (null if there is none attached).

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

}
}

}
}

最佳答案

您需要统一显示游戏对象的位置,它们彼此不认识,您必须始终使用:

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 层次结构

enter image description here

如果您只是用它开始您的旅程,请学习 MonoBehaviour call orderlife cycles of events

关于c# - animation.Play() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33597101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com