gpt4 book ai didi

c# - GameObject.FindGameObjectsWithTag() 不工作

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

我正在尝试查找所有带有“Guard”标签的游戏​​对象。出于某种原因,这是行不通的。我已经用其他标签尝试过,我知道有带有“Guard”标签的对象。

我的代码

GameObject[] guards;

void Start()
{
guards = GameObject.FindGameObjectsWithTag("Guard");
print(guards + name);
}

我知道这可能真的很简单,但打印它总是得到一个空数组。我是 Unity 初学者,非常感谢您的帮助。谢谢你! :)

最佳答案

你怎么知道它是空的?

如果您的意见是基于打印输出,那您就错了。 Print 将始终只抛出一种类型的守卫变量和此脚本附加到的 gameObject 的名称。如果你想检查找到的游戏对象的数量,你应该使用 guards.Length 或公开一个变量,这样你就可以在运行时从 Inspector 中查看它。

public GameObject[] guards;

void Start ()
{
guards = GameObject.FindGameObjectsWithTag("Guard");
print(guards.Length);
}

关于c# - GameObject.FindGameObjectsWithTag() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55943692/

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