我有一个Controller类,它有一个GameObjects列表,它确实是预制的(在我的代码中称为turtlesTypePrefab)。正如您在这里看到的,我使用的预制板里面有带有动画的精灵(我的意思是FirstTurtle有一个动画师,SecondTurtle等等,它们都是我的预制片ThreeTurtles的一部分)。

所以现在在我的代码中,我想改变我的预制件中的一个动画器中的一个布尔值。
在我的控制器里我有:
public GameObject[] turtlesTypePrefab;然后,在我的更新方法中,我想做这样的事情:
void Update()
{
for (int i = 0; i < turtles.Length; i++)
{
for (int j = 0; j < turtles[i].Count; j++)
{
GameObject turtle = turtles[i][j];
if (turtle != null)
{
MoveTurtle(turtle, i);
// THIS DOESNT WORK
anim = turtle.GetComponent<FirstTurtle>().GetComponent<Animator>();
anim.SetBool("diving", true);
}
}
}
}有什么想法吗?
发布于 2020-08-22 22:57:56
如果每个海龟游戏对象都有自己的动画组件,那么您可以从树龟游戏对象GetComponentInChildren()初始化父动画器。
您要做的是通过父级代码访问其中一个子动画器。下面是一个例子
//此脚本将附加到父程序
anim = GetComponentInChildren<Animator>();您使用了以下代码行:
anim = turtle.GetComponent<FirstTurtle>().GetComponent<Animator>();问题在于,FirstTurtle是一个子游戏对象,而不是一个组件。因此,您不能真正将FirstTurtle作为一个组件。使用GetComponent()只在您想要使用您正在处理的游戏对象的动画师时才能工作。所以我建议你使用GetComponentinChildren。
https://stackoverflow.com/questions/63540266
复制相似问题