我正在制作一个统一的 2D 游戏,我有一些预加载的 GameObject 变量,我想在预制件的脚本中引用它们,但我似乎无法使用 GameObject.FindWithTag("VariableName"); (在预制件中加载游戏对象)因为变量是公共ExampleNameExampleVariable而不是公共GameObjectExampleVariable,所以我正在寻找的是如何将ExampleName转换为GameObject,以便我可以使用.FindWithTag();功能它,
public ExampleName ExampleVariable;
public GameObject ExampleVariable;
void Start()
{
ExampleVariable = ExampleName.FindWithTag("<GameObjectsName"); // i have this
ExampleVariable = GameObject.FindWithTag("<GameObjectsName"); // i need this
}
我还没有真正尝试太多,因为这是我在预制件中引用变量时遇到的另一个问题的解决方案,但我觉得这是解决问题的最佳方法,因此我们将不胜感激。
找到了解决方案,您可以使用 FindObjectOfType 函数来代替使用 .FindWithTag 函数:
varibale = (Type)FindObjectOfType(typeof(Type));