c# - GameObject.FindGameObjectsWithTag() 不工作

标签 c# unity3d

我正在尝试查找所有带有“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/

相关文章:

c# - WPF - 程序崩溃

git - Unity 和 git : multiproject structure and symbolic links, 更好的解决方案?

c# - Unity 中的反序列化性能下降

c# - 命名空间与嵌套类

c# - 统一: Wrap mesh softly around other mesh?

c# - Unity 2D - Sprite 在跟随物体时闪烁

c# - Unity 从脚本创建 UI 控件

c# - 何时使用 GetXXX() 方法以及何时使用 Getter 属性

c# - 对包含坐标 x 和 y 的 List<String> 进行排序

c# - 对现有 Entity Framework 对象的 PUT 请求